chop Posted October 19, 2016 Share Posted October 19, 2016 (edited) 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 October 19, 2016 by chop Link to comment Share on other sites More sharing options...
chop Posted October 21, 2016 Author Share Posted October 21, 2016 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 Link to comment Share on other sites More sharing options...
Larry Posted November 6, 2016 Share Posted November 6, 2016 lol. Yeah...this one bites you in the butt many many times before you finally learn a lesson. Happened to me, too! Link to comment Share on other sites More sharing options...
Recommended Posts