PHP setcookie() cannot modify header information 的解决方法_PHP教程

WBOY
Release: 2016-07-21 15:48:00
Original
845 people have browsed it

使用setcookie()函数时总是报以下错误:
Warning: Cannot modify header information - headers already sent by....
解决办法如下:
方法一:
在PHP里Cookie的使用是有一些限制的。
1、使用setcookie必须在标签之前
2、使用setcookie之前,不可以使用echo输入内容
3、直到网页被加载完后,cookie才会出现
4、setcookie必须放到任何资料输出浏览器前,才送出
.....
由于上面的限制,在使用setcookie()函数时,学会遇到 "Undefined index"、"Cannot modify header information - headers already sent by"…等问题,解决办法是在输出内容之前,产生cookie,可以在程序的最上方加入函数 ob_start();
ob_start :打开输出缓冲区
函数格式:void ob_start(void)
说明:当缓冲区激活时,所有来自PHP程序的非文件头信息均不会发送,而是保存在内部缓冲区。为了输出缓冲区的内容,可以使用ob_end_flush()或flush()输出缓冲区的内容。
方法二:
解决Warning: Cannot modify header information - headers already sent by ......
前几天装了个php的大头贴系统测试,发现报错Warning: Cannot modify header information - headers already sent by ......
今天又装openads,还是出现这个问题。怒了。上网找了半天,有人说要在文件开头写上
ob_start();
失败。
后来打开 php.ini 然后把 output_buffering 设为 on 。重起appache,OK。看来这才是解决办法。
特别注意:
如果使用utf-8编码,一定要去掉UTF-8中的BOM,这都是因为utf-8编码文件含有的bom原因,而php4,5都是不支持bom的。去掉bom,可以用Notepad++打开转换一下。

www.bkjia.com true http://www.bkjia.com/PHPjc/319841.html TechArticle 使用setcookie()函数时总是报以下错误: Warning: Cannot modify header information - headers already sent by.... 解决办法如下: 方法一: 在PHP里Cookie的使用是...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!