I have Plesk Server where PHP is running as a CGI.
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>";
}
The above script should prompt the user name and password. (Yes it does)
After entering any user/pass it should print them. (No, it always ask for the user/pass)
How to fix it?
4
Answers
Did you considered using more standard .htaccess / .htpassword files (if you’re using Apache)?
http://httpd.apache.org/docs/2.0/howto/auth.html
Here $_SERVER[‘PHP_AUTH_USER’] looks to be never setted, so it will always display this popup.
Here is the file I include before anything in any project that is under development:
Just include it in your
index.php
before anything else.I’ve got this problem too on one of my servers and the following solved it for me. It’s apparently due to using CGI.
http://www.besthostratings.com/articles/http-auth-php-cgi.html
.htaccess:
PHP:
Try adding beow line to your .htaccess file