php setcookie,无法修改标题信息 - 标题已发送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2658083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:14:20  来源:igfitidea点击:

setcookie, Cannot modify header information - headers already sent

phphttpcookieshttpresponse

提问by Nano HE

I am new to PHP, I practised PHP setcookie() just now and failed.

我是 PHP 新手,我刚刚练习了 PHP setcookie() 并失败了。

http://localhost/test/index.php

http: //localhost/test/index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>

    </head>
    <body>
     <?php
     $value = 'something from somewhere';

     setcookie("TestCookie", $value);
     ?>
    </body>
</html>

http://localhost/test/view.php

http: //localhost/test/view.php

<?php
 // I plan to view the cookie value via view.php
 echo $_COOKIE["TestCookie"];

?>

But I failed to run index.php, IE warning like this.

但是我运行index.php失败,IE这样的警告。

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\index.php:9) in C:\xampp\htdocs\test\index.php on line 12

I enabled my IE 6 cookie no doubt.

毫无疑问,我启用了我的 IE 6 cookie。

Is there anything wrong on my procedure above? Thank you.

我上面的程序有什么问题吗?谢谢你。

WinXP OS and XAMPP 1.7.3 used.

使用 WinXP 操作系统和 XAMPP 1.7.3。

回答by kennytm

The warning is clear.

警告是明确的。

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\index.php:9) in C:\xampp\htdocs\test\index.php on line 12

警告:无法修改标头信息 - 标头已由(输出开始于 C:\xampp\htdocs\test\index.php:9)在 C:\xampp\htdocs\test\index.php 第 12 行发送

Cookies are sent in the HTTP response header. Since the HTML content already started, you cannot go back to the header and add the cookie.

Cookie 在 HTTP 响应标头中发送。由于 HTML 内容已经开始,您无法返回标题并添加 cookie。

From http://php.net/setcookie:

http://php.net/setcookie

setcookie()defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent beforeany output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html>and <head>tags as well as any whitespace.

setcookie()定义了一个与其余 HTTP 标头一起发送的 cookie。与其他标头一样,cookie 必须在脚本的任何输出之前发送(这是协议限制)。这要求您在任何输出之前调用此函数,包括<html><head>标签以及任何空格。

Move that setcookiestatement before any HTML appears:

setcookie在任何 HTML 出现之前移动该语句:

<?php
 $value = 'something from somewhere';

 setcookie("TestCookie", $value);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
....

回答by Sebastian Paaske T?rholm

Cookies are sent in the headers of the transmission of the HTTP page. Once you give some output, you cannot modify these anymore.

Cookie 是在 HTTP 页面传输的标头中发送的。一旦你给出了一些输出,你就不能再修改它们了。

The problem in your case lies in you outputting some of the HTML-document before trying to set the cookie.

您的情况的问题在于您在尝试设置 cookie 之前输出了一些 HTML 文档。

There are a few ways to solve it; one of which is setting the cookie prior to outputting anything on the page like so

有几种方法可以解决它;其中之一是在像这样在页面上输出任何内容之前设置 cookie

<?php
    $value = 'something from somewhere';
    setcookie("TestCookie", $value);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>

    </head>
    <body>

    </body>
</html>

Alternatively, you could buffer your output so that nothing gets written until you explicitly tell it to

或者,您可以缓冲您的输出,以便在您明确告诉它之前不会写入任何内容

<?php
    ob_start(); // Initiate the output buffer
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>

    </head>
    <body>
     <?php
         $value = 'something from somewhere';
         setcookie("TestCookie", $value);
     ?>
    </body>
</html>
<?php
    ob_end_flush(); // Flush the output from the buffer
?>

For more information on this last approach, take a look at the ob_startand ob_end_flushfunctions.

有关最后一种方法的更多信息,请查看ob_startob_end_flush函数。

It might be useful to read about setcookie, too.

阅读有关setcookie 的信息也可能很有用。

回答by digitalpbk

Or just turn

或者只是转

output_buffering = On

in your php.ini

在你的 php.ini

Refer http://digitalpbk.com/php/warning-cannot-modify-header-information-headers-already-sentfor a full solution

有关完整解决方案,请参阅http://digitalpbk.com/php/warning-cannot-modify-header-information-headers-already-sent

回答by Hyman Sleight

You're sending some HTML before you set the cookie. The cookie must be set before sending any output, as it is sent with the response headers. Do this:

您在设置 cookie 之前发送了一些 HTML。必须在发送任何输出之前设置 cookie,因为它与响应标头一起发送。做这个:

<?php
$value = 'something from somewhere';
setcookie("TestCookie", $value);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>

    </head>
    <body>

    </body>
</html>

回答by Steve

Here's something worth looking out for...

这里有一些值得关注的东西......

I had the same problem and found there was a SPACE after the closing ?>at the end of a file I was including in the correct place before any output was being generated. It was driving me mad!

我遇到了同样的问题,发现?>在文件末尾关闭后有一个空格,我在生成任何输出之前将其包含在正确的位置。它让我发疯!