Page 1 of 1

My simple authentication should work!

Posted: Wed Jan 27, 2010 6:07 pm
by JJJJJJ
Hi there,

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 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

Re: My simple authentication should work!

Posted: Thu Jan 28, 2010 12:31 am
by aneesme
Your code works for me. However you can use this piece of code to see what is entered.

Code: Select all

if(isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['PHP_AUTH_PW'])) {
    echo $_SERVER['PHP_AUTH_USER'].' : '.$_SERVER['PHP_AUTH_PW']; die;
}
Regards,
Anees
Amoeba Solution Kiosk

Re: My simple authentication should work!

Posted: Thu Jan 28, 2010 5:08 am
by JJJJJJ
Hi Anees,

Thank you very much for your reply. It's very interesting that it's working for you!

I have now tried a couple of different authentication scripts and I still get this problem of the request for username and password come up again and again even though the right info has been entered. There must be some settings that I have wrong, or maybe it is my hosting(?) with godaddy.

Cheers anyway

Regards

Joe

Re: My simple authentication should work!

Posted: Fri Jan 29, 2010 5:28 am
by JJJJJJ
Hi there,

Just in case anyone reads this post and is interested. I think the problem stems from the fact that I am using godaddy as a host. I googled around and found people had had reasonably similar experience. I downloaded a wamp package and it works fine on that. 8)