setcookie,无法修改头信息 - 已发送的头 [英] setcookie, Cannot modify header information - headers already sent

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

问题描述

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



http:// localhost / test / index.php

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

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

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

http:// localhost / test / view.php

 <?php 
//我计划通过view.php查看cookie值
echo $ _COOKIE [TestCookie];

?>

但我无法运行index.php,IE警告这样。

 警告:无法修改标题信息 - 已发送的标题(输出开始于C:\ xampp\htdocs\test\index.php:9 )in C:\ xampp \htdocs\test\index.php on line 12 

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



我的程序有什么问题吗?非常感谢。



使用WinXP OS和XAMPP 1.7.3。


警告:无法修改标题信息 - 已经发送的标题at C:\ xampp \htdocs\test\index.php:9)in C:\ xampp\htdocs\test\index.php on line 12


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



http://php.net/setcookie


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


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

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

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


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

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

<?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.

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.

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

WinXP OS and XAMPP 1.7.3 used.

解决方案

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

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.

From 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 before any 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.

Move that setcookie statement before any HTML appears:

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

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

这篇关于setcookie,无法修改头信息 - 已发送的头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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