标头已经通过刷新函数发送了吗?

发布于 2024-12-09 15:03:27 字数 2177 浏览 0 评论 0原文

我收到一个有趣的错误,表明标头已通过 ob_end_flush() 发送;当我运行下面的代码时。这就是我收到的确切错误

警告:无法修改标头信息 - 已由 /export/home/ua/games/ 中的(输出从 /export/home/ua/games/gamescripts/login_backend2.php:47 开始输出)发送的标头第 57 行的 gamescripts/login_backend2.php 应该已经重定向,

第 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);
}
 ?>

I got an interesting error which says that headers were already sent through ob_end_flush(); when I run the below code. This is the exact error I am getting

Warning: Cannot modify header information - headers already sent by (output started at /export/home/ua/games/gamescripts/login_backend2.php:47) in /export/home/ua/games/gamescripts/login_backend2.php on line 57 should have redirected by now

line 47 is ob_end_flush();

Edit: Sorry I was a bit careless while pasting my code. I have now arranged it in a right format.

<?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);
}
 ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿看小鸭子会跑 2024-12-16 15:03:27

flush() 发送已排队的标头(本例中为 cookie)。删除 flush() 调用。此代码将引发有关无法输出第二个标头的错误:

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

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

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

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

flush() sends the headers already queued up (the cookies in this case). Remove the flush() call. This code will throw an error about not being able to output the 2nd header:

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

You can also remove the output buffering - it does nothing useful here. This code works fine, however:

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

Only flush() actually sends headers.

鹿港小镇 2024-12-16 15:03:27

确保开始 标记之前没有任何空格或换行符。

Make sure you do not have any spaces or newline characters before the opening <?php tag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文