<?php
/*
 * HTTP Authentication implemented in PHP
 *
 * Version 1.0 - February 2001  - simple example with PHP causing the
 *                                HTTP auth dialogue to appear
 * Version 2.0 - September 2002 - added a simple forced logout
 * Version 3.0 - September 2005 - Rewritten and integrated with sessions
 *                                to maintain state and provide a functional
 *                                usable example
 * (c) 2001-2005 Paul Gregg <pgregg@pgregg.com>
 * http://www.pgregg.com
 *
 * Open Source Code:   If you use this code on your site for public
 * access (i.e. on the Internet) then you must attribute the author and
 * source web site: http://www.pgregg.com/projects/
 * You must also make this original source code available for download
 * unmodified or provide a link to the source.  Additionally you must provide
 * the source to any modified or translated versions or derivatives.
 *
 *
 * Method used, combining sessions with HTTP Auth in order to maintain
 * state.  The difficulty surrounding HTTP Auth is that even after you
 * "logout", the browser will continue to send the correct username
 * and password with each request. Thus immediately logging you back in
 * again - unless you use the states to keep track carefully.
 *
 * In this example we have two session variables to maintain state
 * and we tell the page that we want to login our logout via an argument
 * in the query string, e.g. ?login  ?logout
 * The two state variables are:
 *  LOGGEDIN - Very simple state - either you are logged in or not
 *  LOGGEDOUT - Will be TRUE if we have logged out. It's primary purpose
 *              is to scupper the browser provided password and prevent
 *              the authentication routines from running.
 *              It gets reset to FALSE when we want to login
 *
 * Additional benefits to this method are that we only need to authenticate
 * upon login once.  Normal code implemented HTTP Auth routines authenticate
 * with every page request
 *
 */

// Set up our sessions
if (!isset($_SESSION))              session_start();
if (!isset(
$_SESSION['LOGGEDIN']))  $_SESSION['LOGGEDIN'] = FALSE;
if (!isset(
$_SESSION['LOGGEDOUT'])) $_SESSION['LOGGEDOUT'] = FALSE;

// Simple example authentication function - you will want to replace this
// with something that suits you.
Function checkpw ($user$pass) {
  if (
$user == "paul") {
    if (
$pass == "gregg") {
      return 
TRUE;
    }
  }
  return 
FALSE;
}

// Check to see if this is a logout request
// If so then log us out by setting the session state correctly
if ($_SESSION['LOGGEDIN']===TRUE && preg_match("/logout/i"$_SERVER['QUERY_STRING'])) {
  
$_SESSION['LOGGEDIN'] = FALSE;
  
$_SESSION['LOGGEDOUT'] = TRUE;
  unset(
$_SESSION['PHP_AUTH_USER']);
}

// If we are actually logged out then scupper the browser provided password
if ($_SESSION['LOGGEDOUT']===TRUE$_SERVER['PHP_AUTH_PW'] = '';

// If we aren't logged in, and the state tells us we aren't actively logged
// out then let us check for a successful login
if ($_SESSION['LOGGEDIN'] === FALSE && $_SESSION['LOGGEDOUT'] === FALSE) {
  if ( isset(
$_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']) ) {
    
$_SESSION['LOGGEDIN'] = checkpw($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
    if (
$_SESSION['LOGGEDIN'] === TRUE) {
      
$_SESSION['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_USER'];
    } else {
      unset(
$_SESSION['PHP_AUTH_USER']);
    }
  }
}

// If we aren't logged in and the url contains "login", then the user
// wants the HTTP Auth dialog box
// However before we send it, update the session state to remove the
// forced logout state - this will allow the dialog box to reset the
// browser credentials and enable the credentials to be checked in the
// next page
if ($_SESSION['LOGGEDIN']===FALSE
    
&& preg_match("/login/i"$_SERVER['QUERY_STRING'])) {

  
$_SESSION['LOGGEDOUT'] = FALSE;
  if (!isset(
$HTTP_AUTH_REALM)) $HTTP_AUTH_REALM "User Authorisation";
  
Header("HTTP/1.1 401 Authorization Required");
  
Header("WWW-Authenticate: Basic realm=\"$HTTP_AUTH_REALM\"");
  
Header("Connection: close");
  
Header("text/html");
  print <<<EO401
<HTML><HEAD>
<TITLE>401 Authorization Required</TITLE>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
</HEAD><BODY>
<H1>Authorization Required</H1>
This server could not verify that you
are authorized to access the document you
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.<P>
</BODY></HTML>
EO401;
  exit;
}
?>