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");之后页面就出现上面的错误。上网查了一些资料,说是我的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();//输出全部内容到浏览器 
?>

时间: 2024-08-28 11:56:35

PHP:Cannot modify header information - headers already sent by出错解决的相关文章

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"; pri

出现Warning: Cannot modify header information - headers already sent by ..的解决办法

这个错误的出现,原因是: 出这个错误是因为 header('Content-Type:text/html;charset= UTF-8');发送头之前不能有任何输出. 检查了下我的输出,错误信息如下: Warning: Cannot modify header information - headers already sent by (output started at /data/home/qxu1084910324/htdocs/bw_feature.php:1) in /data/hom

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

这篇文章主要介绍了PHP错误Warning: Cannot modify header information - headers already sent by解决方法,需要的朋友可以参考下 今天在测试以下代码时遇到该错误: 复制代码代码如下: session_start();$_SESSION['username']=$username;echo "<script language='javascript'>location.href='../admin.php';</sc

关于报错:Warning: Cannot modify header information - headers already sent by (output started at

8月5日,第一个项目即将完成,测试时,发现登录功能会出现小问题:记住密码的时候会报错 Warning: Cannot modify header information - headers already sent by (output started at 经过再三在百度上查询,终于查到错误的关键原因:[setcookie之前不可以有html标签内容].导致出现这种错误的方式有很多种,但惟独这种解释最让我理解. (因为百度知道的内容不可以复制了,只能贴出地址,有详细解释)地址[http://z

PHP程序错误Warning: Cannot modify header information - headers already sent by ....

在使用phpmywind程序时出现的问题Warning: Cannot modify header information - headers already sent by .... 检查了许久,居然是编码的问题 请将单纯的utf-8格式改为utf-8无BOM编码格式 在NOTEPAD++的格式菜单可以修改 或者其它工具的

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

一般来说在header函数前不能输出html内容,类似的还有setcookie() 和 session 函数,这些函数需要在输出流中增加消息头部信息.如果在header()执行之前有echo等语句,当后面遇到header()时,就会报出 “Warning: Cannot modify header information - headers already sent by ....”错误.就是说在这些函数的前面不能有任何文字.空行.回车等,而且最好在header()函数后加上exit()函数.例

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 removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unkno

Cannot modify header information - headers already sent by

有时候你在使用 header("Location: http://localhost/myblog/index.php/manager/listview");的时候会出现这个问题,可是也许你是从别处拷贝过来的别处都没有出现过这个问题,这时候你就很疑惑了注意很可能是因为你在前面输出了一个echo的缘故或者你在前面加上一个ob_start():可能够解决

PHP cannoy modify header information - headers already sent by ....

我采用的是MVC模式的写法,代码和html分离的写法 <?php require '../mysql_connect.php'; require('../model/functions.php'); include_once '../views/login.html.php'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { // 处理表单 if (isset($_POST['login_submit'])) { $role = $_POST['role'