1


I have got a php code down below and i want it to have that if the statement if false, it will go to a page automatically.
Whenever i am using the "header" function, i get an error "Cannot modify header information - headers already sent by...."

<?php
if(empty($_SESSION['pass'])) 
{ 
echo "U bent niet ingelogt.";
echo '<form action="index.php" method="post">';
echo 'Code <input type="text" name="code">';
echo '<input type="submit">';
echo '</form>';
} 
else {
// Go automatically to a page;
}
?>
  • Yes, the error means, there was probably some output before the function call. Can you change the place of the if? If not, what alternatives have you come up with? – kero Dec 02 '13 at 10:02
  • @kingkero: well, if the place of this decision cannot be replaced before the output, the application must have a bad design. – mcserep Dec 02 '13 at 10:04
  • I've only tried adding a header to redirect but I got an error that says "Cannot modify header information - headers already sent by". I can't find a other way to make it work. What do you mean by change the place? – Guus van Walstijn Dec 02 '13 at 10:05
  • possible duplicate of [Headers already sent by PHP](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – GordonM Dec 02 '13 at 10:08

5 Answers5

2

Use this function to remove header already sent error

 function redirect($url){
        if (headers_sent()){
          die('<script type="text/javascript">window.location.href="' . $url . '";</script>');
        }else{
          header('Location: ' . $url);
          die();
        }    
    }

and call this function in your else statement.

Mushahid Hussain
  • 4,052
  • 11
  • 43
  • 62
  • I am trying this, but the only problem is, when i am logged out, it will ask me to log in, but when i am logged in and go to this page, it will redirect me to the page i want, but my session is empty when i am there, while it shouldn't, and i have nowhere a session_destroy() – Guus van Walstijn Dec 02 '13 at 10:53
1

The error you're receiving means that there is already some content output into the document, e.g. by echo statement or outside the PHP ?> some content <?php.
Make sure you're not outputing any content before the if statement.

If your project is too big, you can try with output buffering.
Let's say, you're outputing some HTML before the if. You can buffer the output at the beginning and echo it at the end:

<?php
ob_start(); // we turn the buffering on
?>

<html>
    <head>
        <!-- ... -->
    </head>
    <body>
        <?php
        if(empty($_SESSION['pass'])) 
        {
            echo "U bent niet ingelogt.";
            echo '<form action="index.php" method="post">';
            echo 'Code <input type="text" name="code">';
            echo '<input type="submit">';
            echo '</form>';
        } 
        else {
            header("Location: some-other-page.html");
            ob_clean();
            die();
        }
        ?>

    some other HTML inside the body
    </body>
</html>

<?php
echo ob_get_clean(); // output the buffer and clean it
?>
matewka
  • 9,912
  • 2
  • 32
  • 43
0

Make sure that you don't have any whitespace before your first <?php tag and try the header method again.

Alternatively, you can cheat (although I'd never personally recommend it, just saying this could 'fix' it), is put a @ infront of the header function so that it ignores the errors.

Albzi
  • 15,431
  • 6
  • 46
  • 63
0

Headers must be sent before any actual output, see the reference for more information: http://php.net/manual/en/function.header.php

mcserep
  • 3,231
  • 21
  • 36
0

The error message described means that some output has been sent to the browser. As soon as output starts being sent to the browser, all headers are also sent and no new ones can be sent from that point on.

You have 2 options:

  1. Re-arrange your code and manage error output to ensure nothing gets sent to the client until you're done with manipulating the headers
  2. Turn on output buffering

The former is more work, but the latter does come with some performance cost.

GordonM
  • 31,179
  • 15
  • 87
  • 129