note 61791 added to features.http-auth

From: Date: Sun, 12 Feb 2006 03:51:24 +0000
Subject: note 61791 added to features.http-auth
Groups: php.notes 
Request: Send a blank email to php-notes+get-103539@lists.php.net to get a copy of this message
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; ---- Server IP: 80.78.242.20 Probable Submitter: 62.178.212.189 ---- Manual Page -- http://www.php.net/manual/en/features.http-auth.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61791 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61791&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61791&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61791&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61791&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61791&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61791&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61791&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61791&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#103539) next »