Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
chop

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_start();
            $_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
            //http://localhost:8888/sites_in_progress/gill_truslowBS4/loggedin.php
             //(which is correct)
            header("Location:$url");
            ob_end_clean();// stop output buffering
	    echo "We have now gone past the header() function and are still at login.php";
            exit();
                
        } else { // Unsuccessful!
            $errors = $data;
        }
            
        
    mysqli_close($dbc);
    
    } // End of the main submit conditional.
    
    include ('includes/login_page.inc.php'); // THIS INCLUDES HEADER.HTML
    ?>
Edited by chop

Share this post


Link to post
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:

Share this post


Link to post
Share on other sites

lol. Yeah...this one bites you in the butt many many times before you finally learn a lesson. Happened to me, too!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...