phpRob Posted January 16, 2012 Share Posted January 16, 2012 Hey all, I'm currently adopting Chapter 13 into my own quotes database mini-site. The first task I've come across is on the login page. After the user has logged in I want it to re-direct to the homepage (index.php) rather than displaying a simple message as the following script does. My question is, how can I re-write the following script as per the book to allow for header() to be called? Would I have to use output buffering, or is there a bette way? <?php // Script 13.5 - login.php /* This page lets people log into the site. */ // Set two variables with default values: $loggedin = false; $error = false; // Check if the form has been submitted: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form: if (!empty($_POST['email']) && !empty($_POST['password'])) { if ( (strtolower($_POST['email']) == [email=""]'me@example.com'[/email]) && ($_POST['password'] == 'testpass') ) { // Correct! // Create the cookie: setcookie('Samuel', 'Clemens', time()+3600); // Indicate they are logged in: $loggedin = true; } else { // Incorrect! $error = 'The submitted email address and password do not match those on file!'; } } else { // Forgot a field. $error = 'Please make sure you enter both an email address and a password!'; } } // Set the page title and include the header file: define('TITLE', 'Login'); include('templates/header.html'); // Print an error if one exists: if ($error) { print '<p class="error">' . $error . '</p>'; } // Indicate the user is logged in, or show the form: if ($loggedin) { print '<p>You are now logged in!</p>'; } else { print '<h2>Login Form</h2> <form action="login.php" method="post"> <p><label>Email Address <input type="text" name="email" /></label></p> <p><label>Password <input type="password" name="password" /></label></p> <p><input type="submit" name="submit" value="Log In!" /></p> </form>'; } include('templates/footer.html'); // Need the footer. ?> Thanks Link to comment Share on other sites More sharing options...
Larry Posted January 16, 2012 Share Posted January 16, 2012 With this particular script, the solution is actually very simple. Just replace this line: $loggedin = true; with the header() call. 1 Link to comment Share on other sites More sharing options...
phpRob Posted January 16, 2012 Author Share Posted January 16, 2012 Oh god yes, that is simple, how did I miss that, silly me! Thank you for the prompt reply though Larry. Link to comment Share on other sites More sharing options...
Larry Posted January 16, 2012 Share Posted January 16, 2012 No problem and you're quite welcome. Link to comment Share on other sites More sharing options...
Recommended Posts