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:
// 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');
// 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>
include('templates/footer.html'); // Need the footer.



Link to comment
Share on other sites


  • Create New...