PHP提示Cannot modify header information - headers already sent by解决方法
2014-09-22 00:00
330 查看
本文实例讲述了PHP提示Cannot modify header information - headers already sent by解决方法,是进行PHP程序设计过程中经常会遇到的问题。本文对此以实例形式分析解决方法。分享给大家供大家参考。具体方法如下:
现来看看这段代码:
访问该PHP文件时提示Warning: Cannot modify header information - headers already sent by
出错的原因:
原因是在php程序的头部加了,header("content-type: text/html; charset=utf-8");之后页面就出现上面的错误。
因为 header('Content-Type:text/html;charset= UTF-8');发送头之前不能有任何输出,空格也不行,你需要将header(...)之前的空格去掉,或者其他输出的东西去掉,如果他上面include其他文件了,你还要检查其他文件里是否有输出。
上网查了一些资料,说是我的php.ini里面的配置出了问题,找到php.ini文件中的output_buffering默认为off的,把它改为on或者任意一个数字,但尝试无结果。
setcookie函数必須在任何资料输出至浏览器前,就先送出
基于上面這些限制,所以執行setcookie()函数时,常会碰到"Undefined index"、"Cannot modify header information - headers already sent by"…等问题,解決"Cannot modify header information - headers already sent by"这个错误的方法是在产生cookie前,先延缓资料输出至浏览器,因此,您可以在程序的最前方加上ob_start()函數。
ob_start()函数用于打开缓冲区,比如header()函数之前如果就有输出,包括回车\空格\换行\都会有"Header had all ready send by"的错误,这时可以先用ob_start()打开缓冲区PHP代码的数据块和echo()输出都会进入缓冲区而不会立刻输出!
通过以下方法,问题得到解决:
希望本文所述对大家PHP程序设计的学习有所帮助。
现来看看这段代码:
<?php ob_start(); setcookie("username","test",time()+3600); echo "the username is:".$HTTP_COOKIE_VARS["username"]."\n"; echo "the username is:".$_COOKIE["username"]."\n"; print_r($_COOKIE); ?>
访问该PHP文件时提示Warning: Cannot modify header information - headers already sent by
出错的原因:
原因是在php程序的头部加了,header("content-type: text/html; charset=utf-8");之后页面就出现上面的错误。
因为 header('Content-Type:text/html;charset= UTF-8');发送头之前不能有任何输出,空格也不行,你需要将header(...)之前的空格去掉,或者其他输出的东西去掉,如果他上面include其他文件了,你还要检查其他文件里是否有输出。
上网查了一些资料,说是我的php.ini里面的配置出了问题,找到php.ini文件中的output_buffering默认为off的,把它改为on或者任意一个数字,但尝试无结果。
setcookie函数必須在任何资料输出至浏览器前,就先送出
基于上面這些限制,所以執行setcookie()函数时,常会碰到"Undefined index"、"Cannot modify header information - headers already sent by"…等问题,解決"Cannot modify header information - headers already sent by"这个错误的方法是在产生cookie前,先延缓资料输出至浏览器,因此,您可以在程序的最前方加上ob_start()函數。
ob_start()函数用于打开缓冲区,比如header()函数之前如果就有输出,包括回车\空格\换行\都会有"Header had all ready send by"的错误,这时可以先用ob_start()打开缓冲区PHP代码的数据块和echo()输出都会进入缓冲区而不会立刻输出!
通过以下方法,问题得到解决:
//在header()之前 ob_start(); //打开缓冲区 echo \"Hellon\"; //输出 header("location:index.php"); //把浏览器重定向到index.php ob_end_flush();//输出全部内容到浏览器 ?>
希望本文所述对大家PHP程序设计的学习有所帮助。
相关文章推荐
- PHP提示Cannot modify header information - headers already sent by解决方法
- PHP错误Warning: Cannot modify header information - headers already sent by解决方法
- PHP错误Warning: Cannot modify header information - headers already sent by解决方法
- PHP错误Warning: Cannot modify header information - headers already sent by解决方法
- PHP错误Warning: Cannot modify header information - headers already sent by解决方法
- php Cannot modify header information - headers already sent by set 解决方法
- codeigniter 错误提示Cannot modify header information - headers already sent by处理方法
- Cannot modify header information - headers already sent by解决方法
- PHP:Cannot modify header information - headers already sent by出错解决
- wdcp 解决Cannot modify header information - headers already sent by 警告的解决方法
- php有些系统会报错或提示 Cannot modify header information - headers already sent by
- php header Cannot modify header information - headers already sent by ... 解决办法
- 当用header方法输出内容时出现“Cannot modify header information - headers already sent by”的解决方法
- 解决PHP Warning: Cannot modify header information - headers already sent by...
- 解决Zend Studio中Cannot modify header information - headers already sent by 的方法
- 错误信息:Warning Cannot modify header information - headers already sent by 的解决方法fi
- 解决Warning: Cannot modify header information - headers already sent by的方法 转
- 帝国cms Warning: Cannot modify header information - headers already sent by...错误【解决方法】
- wordpress在登录后台出现“Warning: Cannot modify header information - headers already sent by”的解决方法
- 当用header方法输出内容时出现“Cannot modify header information - headers already sent by”的解决方法