Hello, I trying to figure out why this code does not work on PHP 4.4.7: if(!isset($_SERVER['PHP_AUTH_USER'])||!isset($_SERVER['PHP_AUTH_PW'])||$_SERVER['PHP_AUTH_USER']!='user'||$_SERVER['PHP_AUTH_PW']!='pass'){ header('WWW-Authenticate: Basic realm="Realm"'); header('HTTP/1.0 401 Unauthorized'); die("Access Denied"); } echo 'passed'; } PHP: This code works fine on PHP 5.2.6 but on PHP 4.4.7 it keeps asking again and again for username and password even if I entered correct. Any ideas what is wrong? Thanks.
Regardless, you should switch to PHP 5. PHP 4 is very insecure, isn't being updated any more and misses out on some powerful features included in 5.
You should really just use sessions; once someone logs in, you can set a session variable: $_SESSION['uid'] = $user_id; and then check to see if it is set on any page that is password protected: if (!isset($_SESSION['uid'])) { $current_page = basename($_SERVER['PHP_SELF']); $location = "login.php?go=" . $current_page; header("location: $location"); }
Don't forget, while using sessions, the file MUST start with session_start(); (outside of comments) for them to properly work. For instance, you can do this manually per each file or you can set some sort of initiating file. E.g.: init.php <?php /** * Blah * Blah * Your comments * herE! * */ session_start(); define("ROOT_PATH", dirname(__FILE__)); // Define script root directory [where this file should be :)] define("INIT_SUCCESS",true); // If this doesn't come up, your script didn't load correctly. ?> PHP: index.php <?php /** * Comments again... * * Let's load the next file BEFORE anything else to get our session_start(); :) * */ require_once("init.php"); if(!defined("INIT_SUCCESS")){ die("Your init.php did not load properly..."); } else { echo("No errors? Success!<br /><br />Your root path is: ".ROOT_PATH); exit; } ?> PHP: Regards, Dennis M.
Obviously. Just make sure you don't have any whitespace before you start your session or output headers. PHP 101.