note 61791 deleted from features.http-auth by aidan

From: Date: Sun, 12 Feb 2006 08:50:57 +0000
Subject: note 61791 deleted from features.http-auth by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-103552@lists.php.net to get a copy of this message
Note Submitter: deloptes at yahoo dot com Reason: bad code ---- Hi, After looking for a solution for a login using the server's .htaccess control mechanism, I found your writings very very helpfull. Unfortunately I got a bit confused, so it took me a while to glue together following. It might be useful to someone thanks and regards ============= PHP CODE FOLLOWS =========== <?php function print_form() { ?> <div> Login: <br /> <form action="http://<?php print $_SERVER['HTTP_HOST'];?>/login/login.php" method="POST" name="login"> <input type="text" name="username"></input> <br /> <input type="password" name="password"></input> <br /> <input type="submit" name="submit" value="login"></input> </form> </div> <?php return true; } function main() { $AuthUserFile = file("/var/www/.htpasswd"); #$AuthGroupFile = file("/path/to/.groups"); #$group = "Members"; $realm = "Vacation"; ############## PAGE CODE ############ header("Expires: Sat, 01 Jan 2000 00:00:00 GMT"); header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); header("Cache-Control: post-check=0, pre-check=0",false); header("Pragma: no-cache"); if ( empty($_POST) ) { #print "<div>Login required 3</div>"; print_form(); } else { $user=$_SERVER['PHP_AUTH_USER']=$_POST['username']; $pass=$_SERVER['PHP_AUTH_PW']=$_POST['password']; if ( $authUserLine = array_shift(preg_grep("/^$user\:/", $AuthUserFile))) { preg_match("/$user:((..).*)$/", $authUserLine, $matches); $authPW = $matches[1]; $salt = $matches[2]; $password = crypt($pass, $salt); if($password != $authPW) { unset($_SERVER['PHP_AUTH_USER']); unset($_SERVER['PHP_AUTH_PW']); unset($_SERVER['REMOTE_USER']); print_form(); } else { header("Location: http://".$_SERVER['PHP_AUTH_USER'].":". $_SERVER['PHP_AUTH_PW']."@".$_SERVER['HTTP_HOST']. "/vacation/mail.php"); } } else { unset($_SERVER['PHP_AUTH_USER']); unset($_SERVER['PHP_AUTH_PW']); unset($_SERVER['REMOTE_USER']); print_form(); } } } # print $_SERVER['PHP_AUTH_USER']."\n"; #print $_SERVER['PHP_AUTH_PW']."\n"; # start main(); exit;

« previous php.notes (#103552) next »