Cannot modify header information - headers already sent by出错的原因
<?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出错的原因的更多相关文章
- 阿里云服务器出现Warning: Cannot modify header information - headers already sent by (output started at 问题的解决方法
阿里云服务器出现Warning: Cannot modify header information - headers already sent by (output started at 问题的解决 ...
- php有些系统会报错或提示 Cannot modify header information - headers already sent by
Warning: Cannot modify header information - headers already sent by (output started at /home/test/do ...
- PHP错误Warning: Cannot modify header information - headers already sent by解决方法
这篇文章主要介绍了PHP错误Warning: Cannot modify header information - headers already sent by解决方法,需要的朋友可以参考下 今天在 ...
- Warning: Cannot modify header information - headers already sent by (output started at
一般来说在header函数前不能输出html内容,类似的还有setcookie() 和 session 函数,这些函数需要在输出流中增加消息头部信息.如果在header()执行之前有echo等语句,当 ...
- PHP:Cannot modify header information - headers already sent by错误的解决方案
<?php ob_start();setcookie("username","test",time()+3600);echo "the user ...
- 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 ...
- 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 ...
- (转)PHP5使用cookie时报错 cannot modify header information - headers already sent by (......)
转自:http://blog.csdn.net/buyingfei8888/article/details/8899797 运行有警告Warning: Cannot modify header inf ...
- 转载: PHP错误:Warning: Cannot modify header information - headers already sent by ...
如果在执行php程序时看到这条警告:"Warning: Cannot modify header information - headers already sent by ....&quo ...
随机推荐
- css设置不允许复制文本内容
之前做一个网上答题的页面时,考虑到要防止考生利用复制粘贴来提高作弊的可能性,就设计了不允许复制.方法也很简单,通过设置CSS 的 user-select就可以达到目的: -moz-user-selec ...
- python中的垃圾回收机制及原理
序言: 来一起看看: 不同于C/C++,像Python这样的语言是不需要程序员写代码来管理内存的,它的GC(Garbage Collection)机制 实现了自动内存管理.GC做的事情就是解放程序员的 ...
- Java 遇到的困难
(1)需求:xml 转 json 依赖的包:commons-beanutils-1.8.3.jarcommons-collections-3.2.1.jarcommons-lang-2.6.jarco ...
- 2018-8-10-使用-Resharper-特性
title author date CreateTime categories 使用 Resharper 特性 lindexi 2018-08-10 19:16:51 +0800 2018-4-25 ...
- Git分支操作步骤
学习操作Git分支,具体如下: - 查看分支 - 创建分支 - 切换分支 - 合并分支 - 解决分支的冲突 方案: Git支持按功能模块,时间,版本等标准创建分支; 分支可以让开发分多条主线同时进行, ...
- BZOJ 5046 分糖果游戏
网页崩溃了 心态也崩溃了 MD劳资写了那么多 题意: 有a,b两个人分糖,每个人都有一个能量值.每个人每一轮可以选择进行两种操作: 1.取走最左边的糖果,补充相应的能量值并获取相应的美味度. 2.跳过 ...
- 未能加载文件或程序集 ICSharpCode.SharpZipLib
Ui调用Webservice ,Webservice调用Bl BL中明明有那个dll文件 后来发现是webservice里面没有 在附加调试的时候,断点在bl层中
- 从React渲染流程分析Diff算法
1.什么是虚拟DOM 在React中,render执行的结果得到的并不是真正的DOM节点,结果仅仅是轻量级的JavaScript对象,我们称之为virtual DOM. 简单的说,其实所谓的virtu ...
- (转)pycharm autopep8配置
转:https://blog.csdn.net/BobYuan888/article/details/81943808 1.pip下载安装: 在命令行下输入以下命令安装autopep8 pip ins ...
- php面试专题---21、MVC框架基本工作原理考察点
php面试专题---21.MVC框架基本工作原理考察点 一.总结 一句话总结: 会的东西快速过,不要浪费时间,生命有限,都是一些很简单的东西. 1.mvc框架单一入口的 优势 是什么? 可以进行统一的 ...