标头已通过刷新函数发送


headers already sent through flush function?

我得到了一个有趣的错误,它说头部已经通过ob_end_flush()发送;运行下面的代码。这就是我得到的错误

警告:无法修改头信息-头已经发送(输出开始于/export/home/ua/games/gamesscripts/login_backend2.php:47)在/export/home/ua/games/gamesscripts/login_backend2.php的第57行现在应该重定向

第47行是ob_end_flush();

编辑:对不起,我在粘贴代码时有点粗心。我现在已经把它按正确的格式排列好了

<?php
include_once('Services_JSON.php');
include 'connect.php';
include_once('functions.php');
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);

$json = new Services_JSON();  
$username=  $_POST["username"];  
$password = $_POST["password"];  
$check=0;  
$query="SELECT * FROM `game_users` WHERE `username` LIKE  '$username'";  
$result = mysql_query($query)  
or  
 die("<br>Query Error: ".mysql_error());  
while($row = mysql_fetch_row($result))  
{  
    $user = $row[2];  
    $pass = $row[3];  
    $name = $row[1];  
        if($username == $user )  
        {  
            if($password == $pass)  
            {    
                              $Day = 86400 + time(); // 1 Day    
                              $name= "name";  
                              $user= "user";  
                              $pass= "password";  
                              ob_start();  
                              //creating cookie for one day  
                              setcookie("username", $user, $Day);  
                              $cookiename = $name.$user;                    
                              setcookie("games", $cookiename, $Day);    
                              createSession($user,$cookiename);  
                              ob_end_flush();  
                              $data = array($user, $pass, $name);           
                              $check=1;  
                              flush();  
                              header("Location:                  http://www.uark.edu/ua/games/gamescripts/Game/Game.html");  
                              die('should have redirected by now');  

            }
        }
}
if($check==0)
{
    $data = array("failed", "failed", "failed","failed");
    echo $json->encode($data);
}
 ?>

flush()发送已经排队的报头(在本例中是cookie)。移除flush()呼叫。这段代码将抛出一个关于无法输出第二个头的错误:

<?php
    header('a: 1');
    flush();
    header('b: 2');
?>

您还可以删除输出缓冲-它在这里没有任何用处。这段代码工作得很好,但是:

<?php
    ob_start();
    header('a: 1');
    ob_end_flush();
    header('b: 2');
?>

只有flush()发送报头。

确保在开始的<?php标记之前没有任何空格或换行符