警告:不能修改标题信息 - 已由ERROR发送的标题 [英] Warning: Cannot modify header information - headers already sent by ERROR

查看:123
本文介绍了警告:不能修改标题信息 - 已由ERROR发送的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:
已由PHP发送的标题


我一直在努力解决这个错误现在有一段时间了。



首先,我只是认为它是空白区域,但经过进一步的研究后,我认为它可能是一个类似于这样的问题:


查找可以在此头语句之前将输出发送给用户的任何语句。如果您找到一个或多个,请更改您的代码以在它们之前移动标题语句。复杂的条件陈述可能会使问题复杂化,但它们也可能有助于解决问题。考虑一下PHP脚本顶部的条件表达式,它尽可能早地确定标题值并将其设置在那里。


I' m猜测包含头文件与header()一起导致了这个问题,但我不知道如何重新排列代码来摆脱这个错误。





 <?php 
$ username = $ password = $ token = $ fName = ;

include_once'header.php';

if(isset($ _ POST ['username'])&& isset($ _ POST ['password']))
$ username = sanitizeString($ _ POST ['username' ]);

$ password = sanitizeString($ _ POST ['password']); //设置临时用户名和密码变量
$ token = md5($ password); //加密临时密码

if($ username!='admin')
{
header(Location:summary.php);
}
elseif($ username =='admin')
{
header(Location:admin.php);
{
header(Location:index.php);
}
elseif($ username =='')
{
header

else
die(< body>< div class ='container'>< p class ='error'>无效的用户名或密码< / p> ;< / DIV>< /体>中);

if($ username ==|| $ token ==)
{
echo< body>< div class ='container'> < p class ='error'>请输入您的使用者名称和密码< / p>< / div>< / body>;
}
else
{
$ query =SELECT * FROM members WHERE username ='$ username'AND password ='$ token'; //查找表中输入的用户名
$ result = mysql_query($ query);
if(!$ result)
die(Database access failed:。mysql_error());
elseif(mysql_num_rows($ result)> 0)
{
$ row = mysql_fetch_row($ result);
$ _SESSION ['username'] = $ username; //设置会话变量
$ _SESSION ['password'] = $ token;

$ fName = $ row [0];
}
}
?>


解决方案

长期的答案是PHP的所有输出脚本应该缓存在变量中。这包括标题和正文输出。然后在你的脚本的末尾做任何你需要的输出。



你的问题的快速解决方案是添加

  ob_start(); 

是脚本中的第一件事情,如果您只需要这个脚本。如果您在所有脚本中都需要它,请将其添加为header.php文件中的第一件。



这会启用PHP的输出缓冲功能。在PHP中,当你输出某些东西时(做一个回显或打印),如果在那个时候必须发送HTTP头。如果打开输出缓冲,则可以在脚本中输出,但PHP不必发送标题直到刷新缓冲区。如果您打开它并且不关闭它,脚本完成运行后,PHP将自动刷新缓冲区中的所有内容。在几乎所有情况下打开它并没有什么坏处,并且在某些配置下可能会增加性能。



如果您有权访问更改您的php。 ini配置文件中,您可以找到并更改或添加以下内容:

  output_buffering = On 

这可以在不需要调用ob_start()的情况下将输出缓冲出去。



详细了解输出缓冲检出 http://php.net/manual/en/book。 outcontrol.php

Possible Duplicate: Headers already sent by PHP

I've been struggling with this error for a while now.

To start with, I just thought it was white space, but after further research I think it might be a problem similar to this:

Look for any statements that could send output to the user before this header statement. If you find one or more, change your code to move the header statement before them. Complex conditional statements may complicate the issue, but they may also help solve the problem. Consider a conditional expression at the top of the PHP script that determines the header value as early as possible and sets it there.

I'm guessing the include header is causing the problem along with the header(), but I'm not sure how to rearrange the code to get rid of this error.

How do I remove the error?

<?php
    $username = $password = $token = $fName = "";

    include_once 'header.php';

    if (isset($_POST['username']) && isset($_POST['password']))
        $username = sanitizeString($_POST['username']);

    $password = sanitizeString($_POST['password']); //Set temporary username and password variables
    $token    = md5("$password"); //Encrypt temporary password

    if ($username != 'admin')
    {
        header("Location:summary.php");
    }
    elseif($username == 'admin')
    {
        header("Location:admin.php");
    }
    elseif($username == '')
    {
        header("Location:index.php");
    }
    else
        die ("<body><div class='container'><p class='error'>Invalid username or password.</p></div></body>");

    if ($username == "" || $token == "")
    {
        echo "<body><div class='container'><p class='error'>Please enter your username and password</p></div></body>";
    }
    else
    {
        $query = "SELECT * FROM members WHERE username='$username'AND password = '$token'"; //Look in table for username entered
        $result = mysql_query($query);
        if (!$result)
            die ("Database access failed: " . mysql_error());
        elseif (mysql_num_rows($result) > 0)
        {
            $row = mysql_fetch_row($result);
            $_SESSION['username'] = $username; //Set session variables
            $_SESSION['password'] = $token;

            $fName = $row[0];
        }
    }
?>

解决方案

The long term answer is that all output from your PHP scripts should be buffered in variables. This includes headers and body output. Then at the end of your scripts do any output you need.

The very quick fix for your problem will be to add

ob_start();

as the very first thing in your script if you only need it in this one script. If you need it in all your scripts add it as the very first thing in your header.php file.

This turns on PHP's output buffering feature. In PHP when you output something (do an echo or print) if has to send the HTTP headers at that time. If you turn on output buffering you can output in the script but PHP doesn't have to send the headers until the buffer is flushed. If you turn it on and don't turn it off PHP will automatically flush everything in the buffer after the script finishes running. There really is no harm in just turning it on in almost all cases and could give you a small performance increase under some configurations.

If you have access to change your php.ini configuration file you can find and change or add the following

output_buffering = On

This will turn output buffering out without the need to call ob_start().

To find out more about output buffering check out http://php.net/manual/en/book.outcontrol.php

这篇关于警告:不能修改标题信息 - 已由ERROR发送的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆