<?
    
/**
     * login4.php
     *
     * A simple login module that lets a user stay logged in
     * by saving username and, ack, password in cookies.
     *
     * David J. Malan
     * Dan Armendariz
     * Computer Science S-75
     * Harvard Extension School
     */

    // enable sessions
    
session_start();

    
// were this not a demo, these would be in some database
    
define("USER""jharvard");
    
define("PASS""crimson");

    
// if username and password were saved in cookie, check them
    
if (isset($_COOKIE["user"]) && isset($_COOKIE["pass"]))
    {
        
// if username and password are valid, log user back in
        
if ($_COOKIE["user"] == USER && $_COOKIE["pass"] == PASS)
        {
            
// remember that user's logged in
            
$_SESSION["authenticated"] = TRUE;

            
// re-save username and, ack, password in cookies for another week
            
setcookie("user"$_COOKIE["user"], time() + 24 60 60);
            
setcookie("pass"$_COOKIE["pass"], time() + 24 60 60);

            
// redirect user to home page, using absolute path, per
            // http://us2.php.net/manual/en/function.header.php
            
$host $_SERVER["HTTP_HOST"];
            
$path rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
            
header("Location: http://$host$path/home.php");
            exit;
        }
    }

    
// else if username and password were submitted, grab them instead
    
else if (isset($_POST["user"]) && isset($_POST["pass"]))
    {
        
// if username and password are valid, log user in
        
if ($_POST["user"] == USER && $_POST["pass"] == PASS)
        {
            
// remember that user's logged in
            
$_SESSION["authenticated"] = TRUE;

            
// save username in cookie for a week
            
setcookie("user"$_POST["user"], time() + 24 60 60);

            
// save password in, ack, cookie for a week if requested
            
if ($_POST["keep"])
                
setcookie("pass"$_POST["pass"], time() + 24 60 60);

            
// redirect user to home page, using absolute path, per
            // http://us2.php.net/manual/en/function.header.php
            
$host $_SERVER['HTTP_HOST'];
            
$path rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
            
header("Location: http://$host$path/home.php");
            exit;
        }
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Log In</title>
  </head>
  <body>
    <form action="<? echo $_SERVER["PHP_SELF"]; ?>" method="post">
      <table>
        <tr>
          <td>Username:</td>
          <td><input name="user" type="text" value="<? echo ($_POST["user"]) ? $_POST["user"] : $_COOKIE["user"]; ?>" /></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input name="pass" type="password" /></td>
        </tr>
        <tr>
          <td></td>
          <td><input name="keep" type="checkbox" /> &nbsp; keep me logged in until I click <b>log out</b></td>
        </tr>
        <tr>
          <td></td>
          <td><input type="submit" value="Log In" /></td>
        </tr>
      </table>      
    </form>
  </body>
</html>