PHP error: Cannot modify header information – head

2019-01-02 16:20发布

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!

标签: php header
5条回答
牵手、夕阳
2楼-- · 2019-01-02 16:37

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:

function Redirect($url) {
        flush(); // Flush the buffer
        ob_flush();
        header("Location: $url"); // Rewrite the header
        die;
    }

Hope this helps someone else having this problem!

查看更多
低头抚发
3楼-- · 2019-01-02 16:38

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.
查看更多
大哥的爱人
4楼-- · 2019-01-02 16:42

If the header.php file "has the banner", then it is presumably outputting some HTML content to the page.

You can't issue HTTP headers after you have outputted content.

查看更多
心情的温度
5楼-- · 2019-01-02 16:44

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?

查看更多
深知你不懂我心
6楼-- · 2019-01-02 16:55

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.

Your error should tell you exactly what line (and what file) is sending the output.

查看更多
登录 后发表回答