Page 1 of 1

HTTP Authentication and PHP

Posted: Thu Aug 08, 2002 10:00 am
by matthiasone
I am trying to have the browser require authentication before loadiing a page.

here is the code that calls that authentication

Code: Select all

require_once("spall_fns.php");
  if (! isset($area))
    authorize();
This is the funtction that is called

Code: Select all

require_once("/usr/home/faog/functions/db_fns.php");
function authorize($_SERVER)
{
  if (!isset($_SERVERї'PHP_AUTH_USER'])) 
  {
    header("WWW-Authenticate: Basic realm='Lufkin.org'");
    header('HTTP/1.0 401 Unauthorized');
    echo 'You are not a register user.  If you need to be please contact the webmaster';
    exit;
  } 
  else 
  {
  	$query = "SELECT * FROM users WHERE username = '".$_SERVERї'PHP_AUTH_USER']."' AND password = PASSWORD('".$_SERVERї'PHP_AUTH_PW']."')";
	$result = query_db($query); 
	$numrow =  mysql_num_rows($result);
	echo $numrow."<BR>";
	if($numrow == 1)
	&#123; 
	  $user = mysql_fetch_array($result);
      load_mainpage($user&#1111;"uid"]);
	&#125;
	else
	  echo 'You cannot be logged due to incorrect username or password.  Please contact the webmaster with any questions';
  &#125;
&#125;
Can anyone help me?

Posted: Thu Aug 08, 2002 10:07 am
by fatalcure
i dont think using "function authorize($_SERVER)" makes $_SERVER avaliable for that function.

Use this inside the fucntion;

global $_SERVER;

I don't know if that will fix your problem, as I have not used authentication before.

Posted: Thu Aug 08, 2002 10:20 am
by matthiasone
fatalcure - the global thing didn't work
I guess I should explain a little more

when I run the code not as a function, but just at a script inside a page it works correctly.

When in the function, I get the 'You are not a register user. If you need to be please contact the webmaster' echo

Posted: Thu Aug 08, 2002 10:26 am
by fatalcure
why dont u try echoing $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] inside the function to see if they're available.

Posted: Thu Aug 08, 2002 11:17 am
by matthiasone
$_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_USER'] appear to be null or empty when I echoed them.

Posted: Thu Aug 08, 2002 6:27 pm
by fatalcure
so those server vars aren't available to the function, do this:

Code: Select all

function authorize() &#123;
     global $_SERVER&#1111;"PHP_AUTH_USER"], $_SERVER&#1111;"PHP_AUTH_PW"];
     //rest of function
&#125;

Posted: Thu Aug 08, 2002 6:37 pm
by matthiasone
well actually I got it working

Thanks for your help

Posted: Fri Aug 09, 2002 2:02 am
by twigletmac
You shouldn't have to global $_SERVER to have it accessible in a function, it's an autoglobal variable. What was your solution matthiasone?

Mac

Posted: Fri Aug 09, 2002 9:20 am
by matthiasone
well I took it out of the function and made it the stand-alone script and placed a header() to the next page if the user logged in right

Posted: Fri Aug 09, 2002 9:46 am
by hob_goblin
you should have changed

Code: Select all

function authorize($_SERVER)
to

Code: Select all

function authorize()
because

a) $_SERVER is an autoglobal.
b) it SHOULD have spit out errors when you called it because it was missing an argument when called