PHP 错误:无法修改标头信息 - 标头已发送 [英] PHP error: Cannot modify header information – headers already sent

查看:37
本文介绍了PHP 错误:无法修改标头信息 - 标头已发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
已由 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

-我有一个 header.php 文件,其中包括所有重要信息,以及页面的横幅.这个 header.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.

-我让它检查会话值以确保允许用户访问某个页面.如果不允许用户在那里,我将他们踢回登录页面

-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!

推荐答案

一旦文本输出到浏览器,就不能使用 header().由于您的 header.php 可能包含输出 HTML,因此 header() 无法使用.

You cannot use header() once text has been output to the browser. As your header.php include presumably outputs HTML, header() cannot be used.

您可以通过以下几种方式解决此问题:

You can solve this in a couple ways:

  • 将 if 语句移到 header 包含上方(这不起作用,正如您在评论中指出的那样,header.php 设置了 uid 会话和其他重要内容).
  • 在脚本顶部调用 ob_start() 来缓冲输出.
  • Move the if statement above the header include (this won't work, as you've indicated in comments that header.php sets the uid session and other vital stuff).
  • Call ob_start() at the top of the script to buffer the output.

这篇关于PHP 错误:无法修改标头信息 - 标头已发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆