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

PHPz
Release: 2018-11-13 14:59:48
Original
1783 people have browsed it

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

今天在测试以下代码时遇到该错误:

session_start();
$_SESSION['username']=$username;
echo "<script language=&#39;javascript&#39;>location.href=&#39;../admin.php&#39;;</script>";
exit();
Copy after login

出现错误:

Warning: Cannot modify header information - headers already sent by...

看了一些网上的方法也没解决,最后在php.ini配置output_buffering默认为4096就没有遇到这个错误了:

output_buffering设置说明:

    Off: 表示关闭PHP输出缓存
    On: 打开无限大的输出缓存
    4096: 打开大小为4096Byte的输出缓存

默认情况下,php buffer是开启的,而且该buffer默认值是4096,即4kb。你可以通过在php.ini配置文件中找到output_buffering配置.当echo,print等输出用户数据的时候,输出数据都会写入到php output_buffering中,直到output_buffering写满,会将这些数据通过tcp传送给浏览器显示。你也可以通过ob_start()手动激活php output_buffering机制,使得即便输出超过了4kb数据,也不真的把数据交给tcp传给浏览器,因为ob_start()将php buffer空间设置到了足够大。只有直到脚本结束,或者调用ob_end_flush函数,才会把数据发送给客户端浏览器。


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!