Jump to content
Larry Ullman's Book Forums

Recommended Posts

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

Share this post


Link to post
Share on other sites

With this particular script, the solution is actually very simple. Just replace this line:

 

$loggedin = true;

 

with the header() call.

  • Upvote 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×