Heim  >  Artikel  >  Backend-Entwicklung  >  hp函数setcookie()报错:Warning: Cannot modify header_PHP教程

hp函数setcookie()报错:Warning: Cannot modify header_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:43:25772Durchsuche

今天在利用php函数setcookie()设置值是提示:Warning: Cannot modify header information headers already sent by错了,立即去gg问了一下,找到N种解决办法下面我来整理一下希望对大家会有所帮助。

快要下班的时候,看到php讨论学习群中有朋友说设置cookie的时候。向他要了代码看了原因!报错
Warning: Cannot modify header information – headers already sent by (output started at cookie1.php:4) in cookie1.php on line 5


ob_start();
setcookie("username","宋岩宾",time()+3600);
echo "the username is:".$HTTP_COOKIE_VARS["username"]." ";
echo "the username is:".$_COOKIE["username"]." ";
print_r($_COOKIE);
?>
Warning: Cannot modify header information - headers already sent by出错的原因
我在php程序的头部加了,
header("cache-control:no-cache,must-revalidate");
之后页面就出现上面的错误,看了N个资料也没有结果。今天偶尔发现原来是我的php.ini里面的配置出了问题,在C:windows下找到php.ini文件
output_buffering默认为off的。我现在把它设为4096就OK了

用于解决显示提示错误,不能按(日期+导出文件数)为文件名的错误信息.
setcookie函数必?在任何?料?出至浏览器前,就先送出
基於上面?些限制,所以?绦?etcookie()函??r,常??龅?quot;Undefined index"、"Cannot modify header information - headers already sent by"…等???,解?Q"Cannot modify header information - headers already sent by"?????的方法是在?生cookie前,先延??料?出至?g?器,因此,您可以在程式的最前方加上ob_start();???函?怠?br /> ob_start()函数用于打开缓冲区,比如header()函数之前如果就有输出,包括回车空格换行都会有"Header had all ready send by"的错误,这时可以先用ob_start()打开缓冲区PHP代码的数据块和echo()输出都会进入缓冲区而不会立刻输出.当然打开缓冲区的作用很多,只要发挥你的想象.可以总结以下四点:

1.用于header()之前

ob_start(); //打开缓冲区
echo "Hellon"; //输出
header("location:index.php"); //把浏览器重定向到index.php
ob_end_flush();//输出全部内容到浏览器
?>

2.phpinfo()函数可获取客户端和服务器端的信息,但要保存客户端信息用缓冲区的方法是最好的选择.
ob_start(); //打开缓冲区
phpinfo(); //使用phpinfo函数
$info=ob_get_contents(); //得到缓冲区的内容并且赋值给$info
$file=fopen('info.txt','w'); //打开文件info.txt
fwrite($file,$info); //写入信息到info.txt
fclose($file); //关闭文件info.txt
?>

3.静态页面技术
ob_start();//打开缓冲区
?>
php页面的全部输出
$content = ob_get_contents();//取得php页面输出的全部内容
$fp = fopen("output00001.html", "w"); //创建一个文件,并打开,准备写入
fwrite($fp, $content); //把php页面的内容全部写入output00001.html,然后……
fclose($fp);
?>

4.输出代码
Function run_code($code) {
If($code) {
ob_start();
eval($code);
$contents = ob_get_contents();
ob_end_clean();
}else {
echo "错误!没有输出";
exit();
}
return $contents;
}

 

看了PHP手册和搜索了原因。得到一下结论

方法一:

在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 ……有人说要在文件开头写上ob_start();失败。

后来打开 php.ini 然后把 output_buffering 设为 on 。重起appache,OK。看来这才是解决办法。
特别注意:(我就是看了这个才解决问题的)

如果使用utf-8编码,一定要去掉UTF-8中的BOM,这都是因为utf-8编码文件含有的bom原因,而php4,5都是不支持bom的。去掉bom,可以用Notepad++打开转换一下。(我就是看了这个才解决问题的)
用PHP的ob_start(); 控制您的浏览器cache 。

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633185.htmlTechArticle今天在利用php函数setcookie()设置值是提示:Warning: Cannot modify header information headers already sent by错了,立即去gg问了一下,找到N种解决办法下面...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn