Simple HTTP authentication class
<?php class httpauth{ private $realm; private $username; private $password; public function __construct($realm, $sub1, $sub2 = NULL){ if(is_string($sub1) && is_string($sub2)){ while($sub1 != $_SERVER['PHP_AUTH_USER'] || $_SERVER['PHP_AUTH_PW'] != $sub2){ header('WWW-Authenticate: Basic realm="'.$realm.'"'); die('Invalid Username/Password'); } }else if(is_array($sub1) && is_null($sub2)){ while(!array_key_exists($_SERVER['PHP_AUTH_USER'], $sub1) || $sub1[$_SERVER['PHP_AUTH_USER']] != $_SERVER['PHP_AUTH_PW']){ header('WWW-Authenticate: Basic realm="'.$realm.'"'); die('Invalid Username/Password'); } }else{ throw new exception('Invalid httpauth parameter types'); } $this->realm = $realm; $this->username = $_SERVER['PHP_AUTH_USER']; $this->password = $_SERVER['PHP_AUTH_PW']; } public function getRealm(){ return $this->realm; } public function getUsername(){ return $this->username; } public function getPassword(){ return $this->password; } }
Just create an new instance of the httpauth class.
__construct($realm, $sub1[, $sub2])
Starts a http authenticated session
$realm = What you want to show in the login box. Should be unique to any other httpauth logins you may have on your domain
Single User:
$sub1 = The username of the user
$sub2 = The password for the user
Multi-User:
$sub1 = Array of users, in where the keys are the usernames and the values are the passwords
getRealm()
Returns the realm
getUsername()
Returns the username used to login
getPassword()
Returns the password used to login
Comments
Add your comment