My simple authentication should work!
Posted: Wed Jan 27, 2010 6:07 pm
Hi there,
I can't understand what is wrong with the following code. It is a very basic authentication code:
I am entering the username and password and the box comes up again and asks for it again and again.
I understand what is meant to be happening in the code and have used the exit() method to display the values for $username and $password. I'm not sure how I can echo out the values that I'm typing in, because the authorization box keeps coming up before anything can be displayed. I have tried in both firefox and IE and with the header() statements both ways round. I have checked and rechecked all the spelling as well.
Does anybody have any ideas how I am going wrong?
Many thanks in advance.
Joe
I can't understand what is wrong with the following code. It is a very basic authentication code:
Code: Select all
<?php
// username and password for authentication
$username = 'j';
$password = 'j';
if(!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || ($_SERVER['PHP_AUTH_USER'] != $username) || ($_SERVER['PHP_AUTH_PW'] != $password)) {
header('WWW-Authenticate: Basic realm="Time Logger"');
header('HTTP/1.1 401 Unauthorized');
exit('Sorry, you must enter a username and password.');
}
?>I understand what is meant to be happening in the code and have used the exit() method to display the values for $username and $password. I'm not sure how I can echo out the values that I'm typing in, because the authorization box keeps coming up before anything can be displayed. I have tried in both firefox and IE and with the header() statements both ways round. I have checked and rechecked all the spelling as well.
Does anybody have any ideas how I am going wrong?
Many thanks in advance.
Joe