标头已通过刷新功能发送?

时间:2011-10-12 03:13:33

标签: php mysql cookies header flush

我收到一个有趣的错误,表示标题已经通过ob_end_flush()发送;当我运行以下代码时。这是我得到的确切错误

警告:无法修改标题信息 - 已在/export/home/ua/games/gamescripts/login_backend2.php中发送的标题(在/export/home/ua/games/gamescripts/login_backend2.php:47处开始输出)第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);
}
 ?>

2 个答案:

答案 0 :(得分:5)

flush()发送已排队的标头(本例中为Cookie)。删除flush()电话。此代码将抛出一个关于无法输出第二个标头的错误:

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

你也可以删除输出缓冲 - 它在这里没有任何用处。但是这段代码运行正常:

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

只有flush()实际发送标题。

答案 1 :(得分:4)

确保在开始<?php代码之前没有任何空格或换行符。