note 47271 added to features.http-auth

From: Date: Tue, 09 Nov 2004 19:05:49 +0000
Subject: note 47271 added to features.http-auth
Groups: php.notes 
Request: Send a blank email to php-notes+get-80315@lists.php.net to get a copy of this message
Don't like passwords at home or simply don't want access with passwords maybe told form one to another... You make a configuration file like that: # Passwort for special IP-Range IP 192.168.0. axel:PGWAiIeUxcHOg sven:ADD1IDbsVHSEo # Following IP works without password (Keyword 'ALL') IP 192.168.0.4 ALL # Passwords for the rest of the world IP ernst:INo9dSzfU5sRU sven:ADD1IDbsVHSEo <? $path_log = "/home/ernst/.htmyway"; // Konfigurationsfile einlesen: $file = file($path_log); $login = FALSE; // Gets TRUE, when login is valid $ip = 'world'; // This is the currend IP while reading the configuration $access = 'world'; // IP from configuration fitting into $_SERVER['REMOTE_ADDR'] foreach ($file as $zeile) { $zeile = trim($zeile); switch(true) { case ( strlen($zeile) == 0 ): // break for empty lines break; case ( substr($zeile, 0, 1) == '#' ): // break for Komments break; case ( substr($zeile, 0, 2) == 'IP' ): // change IP range $ip = substr($zeile, 3); if ($ip == '') $ip = 'world'; if ( ereg("^$ip", $_SERVER['REMOTE_ADDR']) ) // fitting IP { $access = $ip; } break; case ( ereg("(.+):(.*)", $zeile, $reg) ): // scan passwords $logray[$ip][$reg[1]] = $reg[2]; break; case ( $zeile == 'ALL' ): // if no login is needet $logray[$ip]['all'] = 1; } } // <for testing> if ( isset($_GET['logout']) ) { unset($_SERVER['PHP_AUTH_USER']); unset($_SERVER['PHP_AUTH_PW']); } // </for testing> // authorisation test if ( isset($_SERVER['PHP_AUTH_USER']) AND isset($_SERVER['PHP_AUTH_PW']) ) if( isset($logray[$access][$_SERVER['PHP_AUTH_USER']]) ) if( $logray[$access][$_SERVER['PHP_AUTH_USER']] == crypt($_SERVER['PHP_AUTH_PW'], (substr($logray[$access][$_SERVER['PHP_AUTH_USER']], 0, 2))) ) $login = TRUE; if ( isset($logray[$access]['all']) ) $login = TRUE; // send login-form if needet if ( !$login ) { Header("WWW-Authenticate: Basic realm=\"Test Authentication System\""); Header("HTTP/1.0 401 Unauthorized"); echo "You must enter a valid login ID and password to access this resource\n"; exit; } // Your running programm... if ( $login ) { echo "Hello " . $_SERVER['PHP_AUTH_USER']; ?> <BR> <form> <input type='submit' name='logout' value='logout'> </form> <? } ?> ---- Manual Page -- http://www.php.net/manual/en/features.http-auth.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+47271 Delete -- http://master.php.net/manage/user-notes.php?action=delete+47271&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47271&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47271&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47271&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47271&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47271&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+47271&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#80315) next »