Location: PHPKode > projects > PHP Authentication using Uma > uma/authenticator.php
<?
//$Id: authenticator.php,v 1.2 2002/10/07 06:06:10 rampart Exp $
/**
 *
 * Uma (formerly known as Rampart) Example Page
 *
 * Copyright (C) 2002 Vance Consulting LLC
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * is library is distributed in the hope that it will be useful,
 * t WITHOUT ANY WARRANTY; without even the implied warranty of
 * RCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * http://www.vanceconsulting.net/
 * hide@address.com
 *
 * Rampart SourceForge Site: http://rampart.sourceforge.net/
 *
 * Description:
 *	This is the file that you include in the PHP page you
 * 	wish to protect with Rampart. 
 *	
 *	3 session variables get set here. rampartUser, rampartervice, 
 * 	and rampartToken
 *	rampartUser stores the logged in user's  User object.
 *	rampartService store the current Service object being authenticated.
 *	rampartToken is an hash of boolean values for Services, the 
 * 	authentication token so to speak for the current User authenticated. 
 * 	It is your resposibility to set this hash keys. I set them to 
 * 	correspond the id value in the Service table, the primary key. 
 * 
 * 	These files are setup to use _SESSION, _SERVER and whatever 
 * 	nice PHP variables we have. I have made every effort to not
 * 	have to rely on register global vars set. It should be fine I 
 * 	develop with global vars turned off and this example runs fun.
 */

// Through out these example pages you will see of constant called 
// RAMPART_SID. RAMPART_SID is a constant defined in config.php. It is the
// primary key for the Rampart service in the Service table. You will want
// to setup additional ?_SID constants that represent your services. For 
// instance, if you had an a service called BaseballCardInv you could setup
// a constant called BaseballCardInc_SID and set it the primary key of 
// BaseballCardInc_SID in the Service table. The value of this constant
// is used in the rampartToken as a key, that allows the user to be 
// authenticated to various services at once with the same login attempt.

// Get the session going, you can modify these session variables if you want to
// I tried to pick names that wouldn't conflict with anything else you may
// be using. You want to stay consistent with these values, you will use them
// all over the place. 
session_start();
session_register("rampartPage");
session_register("rampartUser");
session_register("rampartService");
session_register("rampartToken");

// serviceId is the PRIMARY KEY (i.e the id column) from the Service
// table for the service you are authenticating against.
// I am using a constant called RAMPART_SID, that's because I defined it
// in my config.php file. You can manually set it here or define a constant
// as I have done. It's up to you.
// Look at the Service table.
$serviceId = RAMPART_SID;

// permissionLevel is the permission required to access this page.
// A permission equal to or greater than the number specified will 
// be able to access this page.
// Look at the Perm table.
$permissionLevel = 1;

// If session token is not TRUE, attempt to authenticate or throw up 
// the login page with an error. 
if (!$_SESSION['rampartToken'][$serviceId] || 
    !$_SESSION['rampartUser']->accessGranted($_SESSION['rampartService'],
					    $permissionLevel)) {
    // If you setup a login form that is not set up to Post it's data
    // you will need to make that change here _GET perhaps instead of _POST
    if ($_POST['uname'] && $_POST['password']) {
	$_SESSION['rampartUser'] = new User(0, $_POST['uname'], 
					   $_POST['password']);
	if ($_SESSION['rampartUser']->isAuthentic()) {
	    // The second paramater on accessGranted is the perm the user
	    // needs to have set in the Perm table, a higher Perm then
	    // specified will work. So for this example anyone with
	    // a perm of 1 or greater will be allowed access.
	    if ($_SESSION['rampartUser']->accessGranted(
					       $_SESSION['rampartService'], 
					       $permissionLevel)) {
		$_SESSION['rampartService'] = new Service($serviceId);
		$_SESSION['rampartToken'][$serviceId] = TRUE;
		// If you have problems with header redirects, meta refreshes
		// can do the trick as well. There may also be times when 
		// you need to put the host in the header redirect - so I've 
		// heard.
		// echo "Please Wait...";
		// echo "<meta HTTP-EQUIV=Refresh CONTENT='0; URL=$myPage'>";
		header("Location: " . $_SESSION['rampartPage']);
		exit;
	    }
	}
	$err = 1;	// access denied
    }
    
    $_SESSION['rampartService'] = new Service($serviceId);
    if ($_SESSION['rampartPage'] != "login.php") $_SESSION['rampartPage'] = 
						$_SERVER['PHP_SELF'];
    header("location:login.php?error=$err");
    exit;
}
?>
Return current item: PHP Authentication using Uma