View Full Version : Authentication does not work on PHP 4.X
ignas2526
Jan 23rd 2009, 9:25 am
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';
}
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.
Panzer
Jan 23rd 2009, 12:28 pm
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.
ignas2526
Jan 24th 2009, 4:21 am
So are there any other ways instead of switching to PHP 5?
GreatMetro
Jan 24th 2009, 1:26 pm
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");
}
Dennis M.
Jan 24th 2009, 2:55 pm
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.
?>
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; }
?>
Regards,
Dennis M.
GreatMetro
Jan 24th 2009, 2:57 pm
Obviously. Just make sure you don't have any whitespace before you start your session or output headers. PHP 101.
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.