. */ ///SETTINGS////////////////////////////////////////////////////////////////////////////////////////////////////// //Login $require_login = false; //Require login? (if false, no login needed) - WARNING!!! $realm = 'music'; //This is used by browser to identify protected area and saving passwords (one_site+one_realm==one_user+one_password) $users = array( //You can specify multiple users in this array 'music' => 'passw' ); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// //MANUAL///////////////////////////////////////////////////////////////////////////////////////////////////////// /* HOWTO * To each file, you want to lock add this line (at begin of first line - Header-safe): * //Password Protection 8') * Protected file have to be php script (if it's html, simply rename it to .php) * Server needs to have PHP as module (not CGI). * You need HTTP Basic auth enabled on server and php. */ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////CODE///////////////////////////////////////////////////////////////////////////////////////////////////////// class HTTP_Auth { function send_auth_headers($realm='') { Header('WWW-Authenticate: Basic realm="'.$realm.'"'); Header('HTTP/1.0 401 Unauthorized'); } function get_current_url($login='logout@') { $proto = empty($_SERVER['HTTPS']) ? $proto = 'http' : $proto = 'https'; return $proto.'://'.$login.$_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF']; } static function check_auth_internal($user, $pass) { //Check if login is succesfull //(U can modify this to use DB, or anything else) return (isset($GLOBALS['users'][$user]) && ($GLOBALS['users'][$user] == $pass)); } function check_auth($user, $pass) { return call_user_func($this->auth_function, $user, $pass); } function unauthorized() { //Do this when login fails //Show warning and die die("$this->cbanner401 - Forbidden\n

401 - Forbidden

\nLogin...\n$this->hbanner"); die(); //Don't forget!!! } function auth($realm) { //Backward compatibility if(isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_PW'] != '') $PHP_AUTH_USER = $_SERVER['PHP_AUTH_USER']; if(isset($_SERVER['PHP_AUTH_PW']) && $_SERVER['PHP_AUTH_PW'] != '') $PHP_AUTH_PW = $_SERVER['PHP_AUTH_PW']; //Logout if(isset($_GET['logout'])) { //script.php?logout Header('HTTP/1.0 302 Found'); Header('Location: '.$this->get_current_url()); } if(!isset($PHP_AUTH_USER)) { //Storno or first visit of page $this->send_auth_headers($realm); $this->unauthorized(); } else { //Login sent if($this->check_auth($PHP_AUTH_USER, $PHP_AUTH_PW)) { //Login succesfull - probably do nothing here } else { //Bad login $this->send_auth_headers($realm); $this->unauthorized(); } } //Rest of file will be displayed only if login is correct } function __construct($realm='private', $require_login=true, $auth_function=false) { //CopyLeft $ver = '2o11-5.0'; $link = 'blog.harvie.cz'; $banner = "Harvie's PHP HTTP-Auth script (v$ver)"; $this->hbanner = "
$banner\n-\n$link\n"; $this->cbanner = "\n"; $this->auth_function=array($this,'check_auth_internal'); if($auth_function) $this->auth_function=$auth_function; if($require_login) { $this->auth($realm); } } } if($require_login) new HTTP_Auth($realm);