本篇文章给大家谈谈Yii2报错Headersalreadysentin,以及errorreadingheader的知识点,同时本文还将给你拓展Cannotmodifyheaderinformation
本篇文章给大家谈谈Yii2 报错 Headers already sent in,以及error reading header的知识点,同时本文还将给你拓展Cannot modify header information - headers already sent by出错_PHP教程、Cannot modify header information – headers already sent by cookie 的解决办法、Cannot send session cache limiter - headers already sent错误解决方法、CI框架 Cannot modify header information - headers already sent by等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- Yii2 报错 Headers already sent in(error reading header)
- Cannot modify header information - headers already sent by出错_PHP教程
- Cannot modify header information – headers already sent by cookie 的解决办法
- Cannot send session cache limiter - headers already sent错误解决方法
- CI框架 Cannot modify header information - headers already sent by
Yii2 报错 Headers already sent in(error reading header)
有时候我们把 Yii2 版本进行升级之后 会报一些莫名其妙的错误,这次就遇到如下错误信息
an Error occurred while handling another error:
exception
''yii\web\HeadersAlreadySentException'' with message ''Headers already sent in /xxxx/xxxx/xxx.php on line 90.'' in /xxxx/xxxx/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /xxxx/xxxx/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /xxxx/xxxx/vendor/yiisoft/yii2/web/ErrorHandler.php(135): yii\web\Response->send()
#2/xxxx/xxxx/vendor/yiisoft/yii2/base/ErrorHandler.php(111):
yii\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\HeadersAlreadySentException))
#4 {main}
查阅了一些资料说,是因为使用 json 输出内容的时候 没有 exit。以前的写法如下
protected function renderJSON($data=[], $msg ="ok", $code = 200)
{
header(''Content-type: application/json'');
echo json_encode([
"code" => $code,
"msg" => $msg,
"data" => $data,
"req_id" => $this->geneReqId(),
]);
return Yii::$app->end();
}
改成如下
protected function renderJSON($data=[], $msg ="ok", $code = 200)
{
$response = Yii::$app->response;
$response->format = Response::FORMAT_JSON;
$response->data = [
"code" => $code,
"msg" => $msg,
"data" => $data,
"req_id" => $this->geneReqId(),
];
return $response;
}
原文地址: Yii2 报错 Headers already sent in
标签: yii2 json header
智能推荐
- 【ZBar】ios 错误 ignoring file xxx missing required architecture x86_64 in file
- Yii2 捕获错误日志
- Python Flask jsonify a Decimal Error
- Redis 服务常见的几个错误解决方案
- Yii2 实现 Mysql 断线重连
--Edited from Rpc
Cannot modify header information - headers already sent by出错_PHP教程
总结,终极处理办法:修改php.ini文件 ; 把output_buffering默认为off的,改成4096 即可。
ob_start();
setcookie("username","宋岩宾",time()+3600);
echo "the username is:".$HTTP_COOKIE_VARS["username"]."\n";
echo "the username is:".$_COOKIE["username"]."\n";
print_r($_COOKIE);
?>
Warning: Cannot modify header information - headers already sent by出错的原因
我在php程序的头部加了,
header("cache-control:no-cache,must-revalidate");
之后页面就出现上面的错误,看了N个资料也没有结果。今天偶尔发现原来是我的php.ini里面的配置出了问题,在C:\windows\下找到php.ini文件
output_buffering默认为off的。我现在把它设为4096就OK了。
用于解决显示提示错误,不能按(日期+导出文件数)为文件名的错误信息.
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()输出都会进入缓冲区而不会立刻输出.当然打开缓冲区的作用很多,只要发挥你的想象.可以总结以下四点:
1.用于header()之前
ob_start(); //打开缓冲区
echo \"Hellon\"; //输出
header("location:index.php"); //把浏览器重定向到index.php
ob_end_flush();//输出全部内容到浏览器
?>
2.phpinfo()函数可获取客户端和服务器端的信息,但要保存客户端信息用缓冲区的方法是最好的选择.
ob_start(); //打开缓冲区
phpinfo(); //使用phpinfo函数
$info=ob_get_contents(); //得到缓冲区的内容并且赋值给$info
$file=fopen(\''info.txt\'',\''w\''); //打开文件info.txt
fwrite($file,$info); //写入信息到info.txt
fclose($file); //关闭文件info.txt
?>
3.静态页面技术
ob_start();//打开缓冲区
?>
php页面的全部输出
$content = ob_get_contents();//取得php页面输出的全部内容
$fp = fopen("output00001.html", "w"); //创建一个文件,并打开,准备写入
fwrite($fp, $content); //把php页面的内容全部写入output00001.html,然后……
fclose($fp);
?>
4.输出代码
Function run_code($code) {
If($code) {
ob_start();
eval($code);
$contents = ob_get_contents();
ob_end_clean();
}else {
echo "错误!没有输出";
exit();
}
return $contents;
}
Cannot modify header information – headers already sent by cookie 的解决办法
Cannot send session cache limiter - headers already sent错误解决方法
在windows下编程,当使用session_start()方法的时候,有时会报
session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/inpublisher/PHP1.PHP:1)这样的错误,说是已经有输出,用编辑器打,前面明明什么都没有,原来在使用AJAX的时候,也出现过这种情况,后来,把这个PHP 文件放到linux 中打开,会发现,在文件的最前面,会出现“锘 ”这样的一个字符(引号内),把它去掉以后,再运行,OK,运行正常。后来在网上搜索一些文件,给的解释是:UTF8文件的BOM(Byte Order Mark)标志,在保存的时候会自动存入!
不管它是干嘛的,现在的目的就是把它去掉,我总结的方法有下面三种:
1、 在Linux下打开,去掉后再保存
2、 用写字板打开,把光标放到最前后回车,然后再敲删除,重新回到第一行,这样最前面的那个字符就会去掉
3、 使用UltraEdit编辑器(很好用的一个编辑器,网上多的是,自己下去),打开高级----配置----Unicode/utf-8 检测,把自动检测UTF-8文件,自动检测没有BOM的Unicode文件等前面的勾全去掉,然后你再打开那个文件,就会发觉“锘 ”这个字符出现了,删除就OK了
Unicode 签名 (BOM) 可在文档中包括字节顺序标记 (BOM)。BOM 是位于文本文件开头的 2 到 4 个字节,可将文件标识为 Unicode,如果是这
样,还标识后面字节的字节顺序。由于 UTF-8 没有字节顺序,因此可以选择添加 UTF-8 BOM。对于 UTF-16 和 UTF-32,这是必需的。
看见没有!如果选了这个选项,就会在页面的最前面输出2到4个字节!
而 session_start() 要求之前没有任何输出给客户端浏览器
===============================================================================================
这个问题很常见,多数是因为在session_start之前有输出了!对于老鸟来说,这个错误基本上不会发生,但是如果你是用DW写代码的,连高手也有可能发生这个错误!
如上面的提示:在第×××文件的第1行,×××文件的第2行,随你看,这两处是不会有任何输出语句的,但是它还是照常提示,现在看我抓的图:
看红色框框住的地方,对,问题就出在这里!
查DW的帮助文件:
包括 Unicode 签名 (BOM) 可在文档中包括字节顺序标记 (BOM)。BOM 是位于文本文件开头的 2 到 4 个字节,可将文件标识为 Unicode,如果是这样,还标识后面字节的字节顺序。由于 UTF-8 没有字节顺序,因此可以选择添加 UTF-8 BOM。对于 UTF-16 和 UTF-32,这是必需的。
看见没有!如果选了这个选项,就会在页面的最前面输出2到4个字节!
CI框架 Cannot modify header information - headers already sent by
a php error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /var/www/fbz/application/models/Content.php:268)
Filename: helpers/url_helper.php
貌似把前面的
$logsession = $this->session->all_userdata();
这句去了也不会报错
回复内容:
a php error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /var/www/fbz/application/models/Content.php:268)
Filename: helpers/url_helper.php
貌似把前面的
$logsession = $this->session->all_userdata();
这句去了也不会报错
因为在使用session前已经有输出了
一般是因为php文件?>结尾后有空格回车
或者使用了echo或print
今天关于Yii2 报错 Headers already sent in和error reading header的介绍到此结束,谢谢您的阅读,有关Cannot modify header information - headers already sent by出错_PHP教程、Cannot modify header information – headers already sent by cookie 的解决办法、Cannot send session cache limiter - headers already sent错误解决方法、CI框架 Cannot modify header information - headers already sent by等更多相关知识的信息可以在本站进行查询。
本文标签: