PHP:Cannot modify header information - headers already sent by出错解决
来源:互联网 发布:广州少年编程培训班 编辑:程序博客网 时间:2024/06/01 13:47
<?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 ......
- 解决Warning: Cannot modify header information - headers already sent by
- Cannot modify header information – headers already sent by解决
- 解决cannot modify header information - headers already sent by (......)。
- 解决cannot modify header information - headers already sent by (......)。
- 解决Warning: Cannot modify header information - headers already sent by..
- 解决【Warning: Cannot modify header information - headers already sent by....】
- php错误"Cannot modify header information - headers already sent by"
- php错误"Cannot modify header information - headers already sent by"
- php 错误Cannot modify header information - headers already sent by
- php出错Warning: Cannot modify header information - headers already sent by (output started at
- 解决PHP Warning: Cannot modify header information - headers already sent by...
- Cannot modify header information - headers already sent by错误解决办法
- Cannot modify header information - headers already sent by错误解决办法
- Cannot modify header information - headers already sent by错误解决办法
- Cannot modify header information - headers already sent by错误解决办法
- Cannot modify header information - headers already sent by错误解决办法
- ios8下的远程推送通知
- Java String StringBuilder StringBuffer
- javascript对话框组件——artDialog
- 修改attachEvent中this指向/匿名事件处理函数解绑方法
- 计算机考研复习资料推荐(转载)
- PHP:Cannot modify header information - headers already sent by出错解决
- Bull Math(大数乘法)
- 使用Handler在子线程中更新UI
- 白话经典算法系列之六 快速排序 快速搞定
- 解决IWAB0489E Error when deploying Web service to Axis runtime报错问题
- keyshot4破解版 64位&32位 免费版
- css使得文本垂直居中
- 微信公众平台开发学习
- 安装phpmyadmin