PHP 错误:无法修改标头信息 - 标头已发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1793482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP error: Cannot modify header information – headers already sent
提问by Marcus
Possible Duplicate:
Headers already sent by PHP
可能重复:
PHP 已发送的标头
So I have this output on my page.. not understanding why I have it popping up. I'm new to php though, so maybe it's something easy to fix
所以我的页面上有这个输出......不明白为什么我会弹出它。我是 php 新手,所以也许它很容易修复
-I have a header.php file, which includes all important info, as well has the banner of the page. This header.php is included on every page.
- 我有一个 header.php 文件,其中包含所有重要信息,以及页面的横幅。此 header.php 包含在每个页面中。
-I have it checking the session value to make sure user is allowed to be at a certain page. If user is not allowed to be there, I kick them back to login page
- 我让它检查会话值以确保允许用户访问某个页面。如果不允许用户在那里,我将他们踢回登录页面
This is where the error comes up though. This is what I have:
这就是错误出现的地方。这就是我所拥有的:
include_once ("header.php");
if ($_SESSION['uid']!='programmer')
{
header('Location: index.php');
echo 'you cannot be here';
exit;
}
The index that it is redirecting to also has the header. So is having these multiple header references giving me this error? I see no other way to do this, and it's driving me nuts!
它重定向到的索引也有标头。那么有这些多个标头引用给我这个错误吗?我认为没有其他方法可以做到这一点,这让我发疯!
回答by ceejayoz
You cannot use header()once text has been output to the browser. As your header.phpinclude presumably outputs HTML, header()cannot be used.
header()一旦文本输出到浏览器,您就不能使用。由于您的header.php包含可能会输出 HTML,header()因此无法使用。
You can solve this in a couple ways:
您可以通过以下几种方式解决此问题:
- Move the if statement above the header include (this won't work, as you've indicated in comments that
header.phpsets the uid session and other vital stuff). - Call
ob_start()at the top of the script to buffer the output.
- 将 if 语句移到标题 include 上方(这将不起作用,正如您在
header.php设置 uid 会话和其他重要内容的注释中指出的那样)。 ob_start()在脚本顶部调用以缓冲输出。
回答by Quentin
If the header.php file "has the banner", then it is presumably outputting some HTML content to the page.
如果 header.php 文件“有横幅”,那么它大概是在向页面输出一些 HTML 内容。
You can't issue HTTP headers after you have outputted content.
输出内容后,您无法发出 HTTP 标头。
回答by keithjgrant
You cannot send any headers after sending any other content. A very likely culprit is extra whitespace after your closing ?>tag in your header.php. It's generally a good practice to omit the closing tag entirely in any script-only php files.
发送任何其他内容后,您无法发送任何标头。一个很可能的罪魁祸首是?>header.php 中结束标记后的额外空白。在任何纯脚本 php 文件中完全省略结束标记通常是一个好习惯。
Your error should tell you exactly what line (and what file) is sending the output.
您的错误应该准确地告诉您发送输出的行(和文件)。
回答by Marcus
Alright, so it's fixed...... not sure how though, maybe somebody can explain why this works all of a sudden.
好的,所以它是固定的......但不确定如何,也许有人可以解释为什么这会突然起作用。
This is my code:
这是我的代码:
include_once ("header.php");
if ($_SESSION['uid']!='programmer') {
if(isset($_SESSION['uid'])) {
echo $_SESSION['uid'];
}
header('Location: index.php');
exit;
}
Let me repeat, it all works now! PHP... why do you work now?
让我再说一遍,现在一切正常!PHP...你为什么现在工作?
回答by citizenen
I ran into a similar error (also seemingly out of nowhere) with respect to a Redirect function which used to be as follows:
关于重定向函数,我遇到了类似的错误(似乎也是突然出现的),它曾经如下:
function Redirect($url) {
flush(); // Flush the buffer
header("Location: $url"); // Rewrite the header
die;
}
Apparently, you also need to add ob_flush();to fully flush out the old header. The new function is:
显然,您还需要添加ob_flush();以完全清除旧标题。新功能是:
function Redirect($url) {
flush(); // Flush the buffer
ob_flush();
header("Location: $url"); // Rewrite the header
die;
}
Hope this helps someone else having this problem!
希望这可以帮助遇到此问题的其他人!

