Jump to content
Larry Ullman's Book Forums

Header() Is Completely Ignoring Me. Was It Something I Said?

Recommended Posts

Here is a snippet of code right out of this book. I have been using it for years and it has always worked. Now, it just seems to die on me. It is supposed to take me from login.php to logged in.php. Instead, it goes to the line right after the header(), prints it and  stops via exit(). The url in the browser still shows I am at login.php. There are no errors generated.


After I log in with login.php (and it stops without going to loggedin.php), I will type the url for loggedin.php directly into the browser and it will get me to loggedin.php with the session already running. I.E., the privileges will all be set.


So why isn't "login.php" redirecting me to "loggedin.php"?

<?php # Script 11.8 - login.php #3 USING SESSIONS
    if (isset($_POST['submitted'])) {
        require_once ('includes/login_functions.inc.php');
        require_once (CONNECTION);
        list ($check, $data) = check_login($dbc, $_POST['email'], $_POST['pass']);
        if ($check) { // OK!			
            // Set the session data:.
            //session_name('glee');// this will refer to the last session by name instead of starting a new one
            ob_start();// start output buffering
            $_SESSION['user_id'] = $data['user_id'];
            $_SESSION['first_name'] = $data['first_name'];
            $_SESSION['last_name'] = $data['last_name'];
            $name = $_SESSION['first_name'].$_SESSION['last_name'];
            // Redirect:
            $url = absolute_url ('loggedin.php');
            //line below isn't working even though the value for $url is
             //(which is correct)
            ob_end_clean();// stop output buffering
	    echo "We have now gone past the header() function and are still at login.php";
        } else { // Unsuccessful!
            $errors = $data;
    } // End of the main submit conditional.
    include ('includes/login_page.inc.php'); // THIS INCLUDES HEADER.HTML
Edited by chop
Link to comment
Share on other sites

I'm getting old...


It's not the code that's at fault, it's the non-code. That is the lack of code in line number 1, which is blank. A blank space outside of any <?php ?> tags is also known as "whitespace".


I swore I'd never fall victim to whitespace again after the first 3 times.


header() cannot follow any whitespace, ever.

header() cannot follow any whitespace, ever.

header() cannot follow ANY whitespace, ever.


Officially, the php documentation:

"Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file"


There. That should do it! It is now my mantra. It will never happen again... really :mellow:

Link to comment
Share on other sites

  • 3 weeks later...

  • Create New...