<?php
ob_start();
setcookie("username","送家",time()+3600);
echo "the username is:".$HTTP_COOKIE_VARS["username"]."\n";
echo "the username is:".$_COOKIE["username"]."\n";
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函数必須在任何資料輸出至浏览器前,就先送出
基於上面這些限制,所以執行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()输出都会进入缓冲区而不会立刻输出.当然打开缓冲区的作用很多,只要发挥你的想象.可以总结以下四点:

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;
}

Cannot modify header information - headers already sent by出错的原因的更多相关文章

  1. 阿里云服务器出现Warning: Cannot modify header information - headers already sent by (output started at 问题的解决方法

    阿里云服务器出现Warning: Cannot modify header information - headers already sent by (output started at 问题的解决 ...

  2. php有些系统会报错或提示 Cannot modify header information - headers already sent by

    Warning: Cannot modify header information - headers already sent by (output started at /home/test/do ...

  3. PHP错误Warning: Cannot modify header information - headers already sent by解决方法

    这篇文章主要介绍了PHP错误Warning: Cannot modify header information - headers already sent by解决方法,需要的朋友可以参考下 今天在 ...

  4. Warning: Cannot modify header information - headers already sent by (output started at

    一般来说在header函数前不能输出html内容,类似的还有setcookie() 和 session 函数,这些函数需要在输出流中增加消息头部信息.如果在header()执行之前有echo等语句,当 ...

  5. PHP:Cannot modify header information - headers already sent by错误的解决方案

    <?php ob_start();setcookie("username","test",time()+3600);echo "the user ...

  6. Warning: Cannot modify header information - headers already sent by ... functions_general.php on line 45

    摘自:有用到 http://blog.csdn.net/besily/article/details/5396268 PHP错误:Warning: Cannot modify header infor ...

  7. php5.6,Ajax报错,Warning: Cannot modify header information - headers already sent in Unknown on line 0

    php5.6ajax报错 Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be remo ...

  8. (转)PHP5使用cookie时报错 cannot modify header information - headers already sent by (......)

    转自:http://blog.csdn.net/buyingfei8888/article/details/8899797 运行有警告Warning: Cannot modify header inf ...

  9. 转载: PHP错误:Warning: Cannot modify header information - headers already sent by ...

    如果在执行php程序时看到这条警告:"Warning: Cannot modify header information - headers already sent by ....&quo ...

随机推荐

  1. 逐行读取txt文件,分割,写入txt。。。上传,下载

    s = [] f  = open('querylist.txt','r') #由于我使用的pycharm已经设置完了路径,因此我直接写了文件名 for lines in f:     ls = lin ...

  2. Java开发桌面程序学习(10)——css样式表使用以及Button使用

    css 样式表使用 javafx中的css样式,与html的有些不一样,javafx中的css,是以-fx-background-color这种样子的,具体可以参考文档JavaFx css官方文档 简 ...

  3. ASE Alpha Sprint - backend scrum 3

    本次scrum于2019.11.7再sky garden进行,持续10分钟. 参与人: Zhikai Chen, Jia Ning, Hao Wang 请假: Xin Kang, Lihao Ran, ...

  4. HashMap、Hashtable和ConcurrentHashMap的区别

    HashTable 底层数组+链表实现,无论key还是value都不能为null,线程安全,实现线程安全的方式是在修改数据时锁住整个HashTable,效率低,ConcurrentHashMap做了相 ...

  5. CreateMutex函数 (转)

    CreateMutex函数 该函数找出当前系统是否已经存在指定进程的实例.如果没有则创建一个互斥体. CreateMutex()函数可用来创建一个有名或无名的互斥量对象,其函数原型为: HANDLE ...

  6. wrapper配置文件详解

    参考资料 http://www.tuicool.com/articles/jqMv2q 文件编码,每个配置文件起始位置必须指定该文件的编码格式 encoding=UTF-8 如果包含配置文件出现问题可 ...

  7. 对Promise的研究3

    Promise.race() Promise.race方法同样是将多个 Promise 实例,包装成一个新的 Promise 实例. const p = Promise.race([p1, p2, p ...

  8. ueditor 复制word里面带图文的文章,图片可以直接显示

    图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码 目前限chrome浏览器使用,但是项目要求需要支持所有的浏览器,包括Windows和macOS系统.没有办 ...

  9. 20180805-Java ByteArrayOutputStream类

    下面的例子演示了ByteArrayInputStream 和 ByteArrayOutputStream的使用: import java.io.* public class ByteStreamTes ...

  10. scau 17967 大师姐唱K的固有结界

    17967 大师姐唱K的固有结界 该题有题解 时间限制:1000MS  内存限制:65535K 提交次数:41 通过次数:8 收入:107 题型: 编程题   语言: G++;GCC;VC Descr ...