I posted this in Coding but it seems like maybe it sould go here.
I am trying to work through Welling/Thomson book and am having trouble with the user authentication part.
This listing works in PHP version 4, but not in version 5. Check these two links out, they are the same identical code, only difference is the extension which directs it to which version of PHP to interpret it:
The login should be "user" and "pass"
This one works fine in php 4: PHP4 Version
But in version 5 it won't accept the authentication!
See: PHP5 Version
Is there something that needs to be activated to get the password to work in version 5?
😕
I am wondering if it is this explode thing that is screwing it up? Any thoughts?
PHP Code:
list($SERVER['PHP_AUTH_USER'], $SERVER['PHP_AUTH_PW']) =
explode(':', base64_decode(substr($HTTP_AUTHORIZATION, 6)));
}
The page code is as follows, any idea where I am going wrong?
PHP Code:
<?php
// if we are using IIS, we need to set $SERVER['PHP_AUTH_USER'] and $SERVER['PHP_AUTH_PW']
if (substr($SERVER_SOFTWARE, 0, 9) == 'Microsoft' &&
!isset($SERVER['PHP_AUTH_USER']) &&
!isset($SERVER['PHP_AUTH_PW']) &&
substr($HTTP_AUTHORIZATION, 0, 6) == 'Basic ' )
{
list($SERVER['PHP_AUTH_USER'], $SERVER['PHP_AUTH_PW']) =
explode(':', base64_decode(substr($HTTP_AUTHORIZATION, 6)));
}
// Replace this if statement with a database query or similar
if ($SERVER['PHP_AUTH_USER'] != 'user' || $SERVER['PHP_AUTH_PW'] != 'pass')
{
// visitor has not yet given details, or their
// name and password combination are not correct
header('WWW-Authenticate: Basic realm="Realm-Name"');
if (substr($SERVER_SOFTWARE, 0, 9) == 'Microsoft')
header('Status: 401 Unauthorized');
else
header('HTTP/1.0 401 Unauthorized');
echo '<h1>Go Away!</h1>';
echo '<p>You are not authorized to view this resource.</p>';
phpinfo();
}
else
{
// visitor has provided correct details
echo '<h1>Here it is!</h1>';
echo '<p>I bet you are glad you can see this secret page.</p>';
phpinfo();
}
?>
Thanks for any help or ideas! 😕