Jump to content



Photo
- - - - -

Header()


  • Please log in to reply
3 replies to this topic

#1 phpRob

phpRob

    Advanced Member

  • Members
  • PipPipPip
  • 58 posts
  • LocationCardiff, South Wales

Posted 16 January 2012 - 9:02 AM

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
  • 0

#2 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,911 posts
  • LocationState College, PA (USA)

Posted 16 January 2012 - 9:12 AM

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

$loggedin = true;

with the header() call.
  • 1

#3 phpRob

phpRob

    Advanced Member

  • Members
  • PipPipPip
  • 58 posts
  • LocationCardiff, South Wales

Posted 16 January 2012 - 9:32 AM

Oh god yes, that is simple, how did I miss that, silly me! Thank you for the prompt reply though Larry.
  • 0

#4 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,911 posts
  • LocationState College, PA (USA)

Posted 16 January 2012 - 12:35 PM

No problem and you're quite welcome.
  • 0