<?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. Android和C#实时视频传输Demo

    说起去年的Demo.以今天的免费整齐优势. 原理很easy,虽然没有写android申请书.但,好了~ 高级语言是相通的.傲慢约.就这么简单研究了一下api后,找到相机对象有一个预览回调方法. 意识到 ...

  2. 应用spss可靠性分析软件

    问卷调查的可靠性分析 一.概念:     信度是指依据測验工具所得到的结果的一致性或稳定性,反映被測特征真实程度的指标. 一般而言,两次或两个測验的结果愈是一致.则误差愈小,所得的信度愈高,它具有下面 ...

  3. Android四种Activity的加载模式(转)

    建议首先阅读下面两篇文章,这样才可以更好的理解Activity的加载模式: Android的进程,线程模型: http://www.cnblogs.com/ghj1976/archive/2011/0 ...

  4. 《R实战》读书笔记三

    第二章  创建数据集 本章概要 1探索R数据结构 2使用数据编辑器 3数据导入 4数据集标注 本章所介绍内容概括例如以下. 两个方面的内容. 方面一:R数据结构 方面二:进入数据或者导入数据到数据结构 ...

  5. 在.NET Fiddle有趣的沙盒代码

    在.NET Fiddle有趣的沙盒代码 笔者:Tony Patton | 托尼·巴顿译:PurpleEndurer,2014-11-18,第1版 C#和VB.NET开发者能够使用.NET Fiddle ...

  6. 【安德鲁斯】于java代码集drawableLeft给予适当的大小如何,当?

    textView.setCompoundDrawables(drawable, null, null, null);如果看不到图片,这是由于需要手动定drawable适当的大小,使用drawable. ...

  7. uva 10228 - Star not a Tree?(模拟退火)

    题目链接:uva 10228 - Star not a Tree? 题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点) 解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处, ...

  8. SQL2000 MD5加密

    原文:SQL2000 MD5加密 /***************************************************************************** * Na ...

  9. Find a way (BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 BFS搜索  目标地  并记录下来  之后再判断两段路程之和 代码: #include < ...

  10. 10个devexpress ASPxPivotGrid常见问题

    原文:10个devexpress ASPxPivotGrid常见问题 1.DXperience ASPxGridView如何开启lightweight模式 描述:ASPxGridView样式主题中pa ...