|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-07-23 08:09 UTC] tgourrier at hotmail dot com
Description:
------------
When using the:
header('WWW-Authenticate: Basic realm="My Realm"');
mechanism, the PHP_AUTH_* variables are set and there is no way to clear or unset these variables if the authentication fails.
This is in contrast to the way that external authentication works (with Apache at least). If external authentication fails, the PHP_AUTH variables are not set (or at least they are cleared).
There should be some way within PHP to clear these variables if the authentication is not successful.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 17:00:02 2025 UTC |
Try the script below with an .htpasswd/.htaccess protection. On my test server unless correct credentials are specified PHP_AUTH variables are not populated. <?php if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authenticate: Basic realm="My Realm"'); header('HTTP/1.0 401 Unauthorized'); echo 'Text to send if user hits Cancel button'; exit; } else { echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>"; echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>"; } ?>I think you do not understand my scenario. My .htpasswd IS in a non-web directory, but this issue has nothing to do with .htpasswd files or .htaccess files. Let me clarify a little more. I have a php page which I would like to users to access both without authenticating. If the users choose to authenticate, they may, and if they do so successfully, then the page will display additional content. If I use the default .htaccess directive "Require user", then the users will be forced to authenticate to view the page. Also, if this were the case, I would not need to use the: header('WWW-Authenticate: Basic realm="My Realm"') command, as the web server would force authentication. In my scenario the authentication is invoked by some action the user takes. This action calls a script which has the header('WWW-Authenticate: ...') command in it. However, this command does not authenticate a user against anything -- it simply collects a username and password. It is then up to the remainder of the script to do the authentication. What needs to be done is after the header() function collects the username and password, run some logic to authenticate the user. If this logic fails, then the user is NOT authenticated. The problem is the PHP_AUTH variables are already set, and there is no way to unset or clear them. Take for example, I have a page that I would like to conditionally secure with a username of "foo" and a password of "fighters". I could use the following script: <?php if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authenticate: Basic realm="My Realm"'); header('HTTP/1.0 401 Unauthorized'); echo 'Text to send if user hits Cancel button'; exit; } else { if ($_SERVER['PHP_AUTH_USER'] == "foo" && $_SERVER['PHP_AUTH_PW'] == "fighters") { //the user is authenticated, continue processing } else { // user authentication has failed and PHP_AUTH // variables should not be set } } ?> Of course, this is a very oversimplified logic for authenticating a user, but hopefully it illustrates my point.