PHP:Cannot modify header information - headers already sent by错误的解决方案
<?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: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等语句,当 ...
- 阿里云服务器出现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 ... 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 ...
- PHP提示Cannot modify header information - headers already sent by解决方法
PHP提示Cannot modify header information - headers already sent by解决方法 因为 header();发送头之前不能有任何输出,空格也不行, ...
随机推荐
- C++学习笔记36 (模板的细节明确template specialization)和显式实例(template instantiation)
C++有时模板很可能无法处理某些类型的. 例如: #include <iostream> using namespace std; class man{ private: string n ...
- MPQ Storm库 源代码分析 一个
MPQ什么? MPQ维基上说的非常明确. 简而言之,它是暴雪公司用于游戏数据打包的工具.星际争霸,魔兽争霸游戏中都有使用.该工具内含游戏资源加密和压缩等功能. git下载地址:http ...
- 【iOS发展-28】制造业UITabBarController标记控制器、定制UITabBarItem文字图像6途径和More评论
一个.一个简单的制作过程(实际工程中不建议这样的方式,不要只展示所用原理的理解) 在AppDelegate.m在: - (BOOL)application:(UIApplication *)appli ...
- Linux Howto
1. Customize the Xfce menu http://wiki.xfce.org/howto/customize-menu
- SqlServer发送邮件,定时作业
今天偶然研究了一下sqlserver发送邮件的功能,之前听说过可以发,但是一直没尝试过,只是用C#写后台程序的方式来发邮件. 现在又多了一种发送邮件的途径. 大致的步骤如下: 1.配置sqlserve ...
- osx launchpad删除图标
安装了个parallels desktop之后,OSX中的launchpad中的图标多了不少,但是好多都不是我自己想要的,我们该怎么删除或者改动呢,以下介绍一些方法: ①直接操作Appications ...
- 创建自定义的Middleware中间件
创建自定义的Middleware中间件 阅读目录 何为Middleware中间件 使用Inline方式注册Middleware 使用Inline+ AppFunc方式注册Middleware 定义原生 ...
- 浏览器HTTP缓存原理分析(转)
章节目录 浏览器缓存原理 文字版描述 一图以蔽之 缓存相关首部字段 request缓存相关首部字段 response缓存相关首部字段 实体首部缓存相关字段 缓存配置的一些注意事项 以前项目中遇到了很多 ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(6)- EF上下文实例管理
原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(6)- EF上下文实例管理 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框架搭建 ( ...
- MPI编程简述
第三章MPI计划 3.1 MPI简单介绍 多线程是一种便捷的模型,当中每一个线程都能够訪问其他线程的存储空间.因此,这样的模型仅仅能在共享存储系统之间移植. 一般来讲,并行机不一定在各处理器之间共享存 ...