header已经传送的意思是: 在用户请求时(就是session_start()那句),检查请求中是否发送了特定的会话 ID。如果是,则之前保存的环境就被重建。也就是说在使用session_start()时,会话已经发送出去了header("Location:index_news.php"); 也发送HTTP Header的方法,所以同理,在他之前也不能有其他输出的,这里手册上有说明: Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.所以header("Location:index_news.php");要放最上面,或者使用ob_start()
原因:PHP先发送完所有的Header,然后终止 HTTP header,再发送主体数据。终止之后再对header操作是不允许的。这个问题有很多相关讨论可以参考。
可以在页面上部加上
ob_start()
来启用output buffering机制
所以如果代码简单的话,通常会把session_start()写在最前面
三行的 Cannot modify header information ,意思都一样,header已经传送了,不能修改
header("Location:index_news.php");
这行代码不是跳转到别的页面么,会有什么影响它跳转的实现呢?
在用户请求时(就是session_start()那句),检查请求中是否发送了特定的会话 ID。如果是,则之前保存的环境就被重建。也就是说在使用session_start()时,会话已经发送出去了header("Location:index_news.php"); 也发送HTTP Header的方法,所以同理,在他之前也不能有其他输出的,这里手册上有说明:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.所以header("Location:index_news.php");要放最上面,或者使用ob_start()
session_start和header函数之前,是不允许出现输出的。如果没有:那么,是否你是utf-8编码,如果是,是否保存为含有DOM的UTF8了,PHP会把文件的前两个字符当成输出。
保存为不含有DOM的便可以了。PS:此DOM非彼DOM,百度一下DOM+UTF8