<?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错误的解决方案的更多相关文章

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

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

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

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

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

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

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

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

  5. 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 ...

  6. 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 ...

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

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

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

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

  9. PHP提示Cannot modify header information - headers already sent by解决方法

    PHP提示Cannot modify header information - headers already sent by解决方法 因为 header();发送头之前不能有任何输出,空格也不行, ...

随机推荐

  1. 兼容的网页宽度margin padding

    hack兼容: -moz-  /* Firefox 4 */ -webkit- /* Safari 和 Chrome */ -o-  /* Opera */ IE6承认*和_和+,不承认!import ...

  2. Meteor全栈开发平台

    Meteor全栈开发平台 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博客地址为http://www.cnblogs.com/jasonno ...

  3. SSL/TLS协议运行机制的概述(转)

    互联网的通信安全,建立在SSL/TLS协议之上. 本文简要介绍SSL/TLS协议的运行机制.文章的重点是设计思想和运行过程,不涉及具体的实现细节.如果想了解这方面的内容,请参阅RFC文档. 一.作用 ...

  4. (大数据工程师学习路径)第四步 SQL基础课程----其他(基础练习到此为止)

    一.准备 在正式开始本内容之前,需要先从github下载相关代码,搭建好一个名为mysql_shiyan的数据库(有三张表:department,employee,project),并向其中插入数据. ...

  5. HDU 4085 Steiner树模板称号

    Dig The Wells Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. HDU 4901 The Romantic Hero(二维dp)

    题目大意:给你n个数字,然后分成两份,前边的一份里面的元素进行异或,后面的一份里面的元素进行与.分的时候依照给的先后数序取数,后面的里面的全部的元素的下标一定比前面的大.问你有多上种放元素的方法能够使 ...

  7. UVA Don&#39;t Get Rooked

    主题如以下:  Don't Get Rooked  In chess, the rook is a piece that can move any number of squaresverticall ...

  8. 【百度地图API】手机浏览器抓包工具及其使用方法

    原文:[百度地图API]手机浏览器抓包工具及其使用方法 摘要:为了测试地图API在手机浏览器上的性能,需要给手机浏览器设置代理.通过代理,我们可以在PC上获取到抓包数据.进而对性能做进一步分析. -- ...

  9. OCP-1Z0-051-名称解析-文章32称号

    32. Which CREATE TABLE statement is valid? A. CREATE TABLE ord_details          (ord_no NUMBER(2) PR ...

  10. PHP_零基础学php_3PHP函数、传参函数、默认参数、函数返回值

    <?php function say_hello() //无参数 { $name="tang"; echo "hello,".$name; echo &q ...