Jump to content


Works in PHP4 but not PHPP5 - please help!

  • Please log in to reply
No replies to this topic

#1 Conjurer

  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 14 October 2006 - 08:37 PM

I am trying to work through Welling/Thomson book on PHP/MySQL Web Development and am having trouble with the user authentication example.

This listing works for me 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 information should be "user" and "pass" for the username and password, respectively.
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:
    explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));

The full page listing follows. Any idea where I am going wrong? Been struggling with this all morning.  :'(

PHP Code:

// if we are using IIS, we need to set $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW']
if (substr($_SERVER['SERVER_SOFTWARE'], 0, 9) == 'Microsoft' &&
    !isset($_SERVER['PHP_AUTH_USER']) &&
    !isset($_SERVER['PHP_AUTH_PW']) &&
    substr($_SERVER['HTTP_AUTHORIZATION'], 0, 6) == 'Basic ' )
    explode(':', base64_decode(substr($_SERVER['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="Real World Server"');
  if (substr($_SERVER['SERVER_SOFTWARE'], 0, 9) == 'Microsoft')
    header('Status: 401 Unauthorized');
    header('HTTP/1.0 401 Unauthorized');

  echo '<h1>Go Away!</h1>';
  echo '<p>You are not authorized to view this resource.</p>';
  // 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>';

Thanks for any help or ideas! :confused:

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users