PHP错误:不能修改标题信息 - 已经发送的标题 [英] PHP error: Cannot modify header information – headers already sent

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

问题描述


可能存在重复:

头部已由PHP发送


所以我有这个输出在我的网页上..不理解我为什么弹出。我是新来的PHP,所以也许这是一件容易解决的问题。



- 我有一个header.php文件,其中包含所有重要的信息,还有横幅的页面。这个header.php包含在每个页面上。



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



这是错误出现的地方。这是我的:

  include_once(header.php); 
$ b $ if($ _SESSION ['uid']!='programmer')
{
header('Location:index.php');
回声'你不能在这里';
出口;
}

重定向到的索引也有标题。那么有这些多头参考给我这个错误?我发现没有其他办法可以做到这一点,并且这让我疯狂了! 您不能使用头()一旦文本输出到浏览器。由于您的 header.php include可能会输出HTML,因此无法使用 header()



你可以通过几种方式解决这个问题:


  • 不起作用,正如你在评论中指出的那样 header.php 设置了uid会话和其他重要的东西)

  • 呼叫 ob_start()可以缓冲输出。

Possible Duplicate:
Headers already sent by 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

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

解决方案

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:

  • 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天全站免登陆