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

时间:2022-09-13 16:56:44

<?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&colon; Cannot modify header information - headers already sent by &lpar;output started at

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

  2. 阿里云服务器出现Warning&colon; Cannot modify header information - headers already sent by &lpar;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&colon; Cannot modify header information - headers already sent by解决方法

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

  5. Warning&colon; Cannot modify header information - headers already sent by &period;&period;&period; functions&lowbar;general&period;php on line 45

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

  6. php5&period;6&comma;Ajax报错,Warning&colon; 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. &lpar;转&rpar;PHP5使用cookie时报错 cannot modify header information - headers already sent by &lpar;&period;&period;&period;&period;&period;&period;&rpar;

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

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

    如果在执行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. DotNet处理服务器路径的方法

    项目中需要使用到路径处理的地方比较多,对于路径的解析和匹配有时较为繁琐,现在提供一个对路径进行解析的方法: 1.验证设置路径字符串: /// <summary> /// 验证设置路径字符串 ...

  2. zstack 离线升级1&period;1到 1&period;2 rc

    说明 zstack版本1.1是通过离线安装的. 升级过程 1 挂载下一个版本的zstack的社区版本centos镜像 ZStack-Community-x86_64-DVD-1.2.0.iso mkd ...

  3. Microsoft Visual Studio Ultimate 2013 with Update 3 CN&plus;EN

    官方90天试用版. Microsoft Visual Studio Ultimate 2013 with Update 3 - 简体中文DVD5 ISO image (SHA-1: 9A306631A ...

  4. &lbrack;课程相关&rsqb;homework-08

    一.变量作用域和生命周期 #include <cstdlib> #include <iostream> using namespace std; void try_change ...

  5. 搭建Tornado&plus;Nginx

    Tornado一个高效的异步非阻塞式的实时Web服务器,是Facebook旗下的 FriendFeed 网站开源Web服务器版本.但是它内置的HTTP服务器功能有限,不能在生产环境下使用. 在 Fri ...

  6. nodejs模块学习: connect解析

    nodejs模块学习: connect解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固,需要开发者创造大量的*来解决 ...

  7. Python3 File 方法

    Python3 File(文件) 方法 file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行 ...

  8. placeholder中文字添加换行方法

    需求: 文本域内的提示文字两行显示 解决方案: 表示回车 表示换行 <textarea id="textarea" maxlength="22" plac ...

  9. 安装owncloud出现:Error while trying to create admin user&colon; An exception occurred while executing

    安装owncloud出现:Error while trying to create admin user: An exception occurred while executing 1.安装ownc ...

  10. Oracle添加定时任务

    1.创建存储过程 注:执行语句后,如果需要请添加commit 2.添加定时job,执行存储过程 declare job_delete number; begin dbms_job.submit( jo ...