Re: Can it be improved upon?

From: Date: Fri, 21 Dec 2001 18:04:31 +0000
Subject: Re: Can it be improved upon?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-78501@lists.php.net to get a copy of this message
You might consider converting users.pass to one of the various DB formats so that you can do a direct lookup on $user. And, move the check for !isset($PHP_AUTH_USER) outside of the while loop. If that condition is going to be true, there's no reason to even bother opening the file. On Fri, 21 Dec 2001, jtjohnston wrote: > Can it be improved upon? > Suggestions welcome, > John > > <?php > > $fp = fopen ("./users.pass", "r"); > > while (!feof ($fp)) { > $line = fgets($fp, 4096); > list($user,$password) = explode(":", $line); > $password = chop($password); > > if(!isset($PHP_AUTH_USER)) > { > authenticate(); > } > elseif(($PHP_AUTH_USER != $user) || ($PHP_AUTH_PW != $password)) > { > authenticate(); > }else{ > echo "<p>Welcome: $PHP_AUTH_USER</p>"; > echo "<p>Password: $PHP_AUTH_PW</p>"; > exit; > } > } > fclose($fp); > > function authenticate() { > global $PHP_AUTH_USER, $PHP_AUTH_PW; > header( "WWW-Authenticate: Basic realm=\"Students\""); > header( "HTTP/1.0 401 Unauthorized"); > echo "$PHP_AUTH_USER, you must enter a valid login ID and password to > access this resource. \"$PHP_AUTH_PW\" is not a valid password!\n"; > exit; > } > > ?> > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#78501) next »