Forum Moderators: coopster

Message Too Old, No Replies

PHP/HTTP Authentication

Tips and general discussion

         

lZakl

10:57 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



I spent quite some time comming up with a suitable, yet basic HTTP Authentication script. I think it works for what I am doing, yet would like some opinions on it, the bads, the worse's and sure, even the goods ;0)

I have a logon for the directory call "widgets". This directory contains semi-sensitive materials, that the general public doesn't need to see. So I have a list of files here:

index.php -- where thbase of my authentication is

include.inc -- an include file, hidden way high up in my server root, above the web root and accessible only with administrative privileges. This file contains my usernames and passwords in comma seperated values ie John,hispass.

and the rest of my files

widgets1.php
widgets2.php
widgets3.php
widgets4.php
widgets5.php .. etc

index.php
contains the following code:

<?php
session_start();
ob_start();

function auth($user, $pass)
{
include ('include.inc');
$users = array();
foreach ($MyArray as $value)
{
list($uid, $pss) = split('[,.-]', $value);
$users[$uid] = $pss;
}
if (isset($users[$user]) && ($users[$user] == $pass))
{
return true;
}

else
{
return false;
}
}

if (! auth($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']))
{
header('WWW-Authenticate: Basic realm="My Website"');
header('HTTP/1.0 401 Unauthorized');
echo "NOT AUTHORIZED!";
exit;
}

$_SESSION['loggedin'] = true;

?>

<html>
Authorized users will see this!
</html>

Now you see the include.inc contains a list of users and passes like mentioned above, however they are listed as such:

<?
$MyArray[] = "user1,pass";
$MyArray[] = "user2,pass";
$MyArray[] = "user3,pass";
$MyArray[] = "user4,pass";
?>

The rest of my files at the top include a small script that checks for authorization... and that is:

<?php
session_start();
ob_start();

if (!isset($_SESSION['loggedin']))
{
$redirect_url = "index.php";
header("Location: $redirect_url");
}
ob_end_flush;
?>

<html>
Authorized users see this!
</html>

#################

I hoped to get this on the web so that there may be a more detailed sample for people who don't get the samples given by php.net and such, as they don't give an example of comparing the username:pass, just the PHP_AUTH basics. It took me a few hours of searching and playing to get this figured out. If there is something I can add, or do to make this better, I would love to hear from you, and others like me searching the web will appreciate it!

-- Zak

StupidScript

11:12 pm on Jun 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Neat!

About the only security note I would make is to use

require ('include.inc');

instead of

include ('include.inc');

include()
would result in a warning if it could not be included for some reason, where
require()
would result in a fatal error that would halt further processing of the page.

jatar_k

11:14 pm on Jun 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I might even say require_once in case you call auth more than once and the file is included inside the function, it would re include for every call.

very nice work though, lZakl

<added>I think I might change this

if (! auth($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']))
{
header('WWW-Authenticate: Basic realm="My Website"');
header('HTTP/1.0 401 Unauthorized');
echo "NOT AUTHORIZED!";
exit;
}

$_SESSION['loggedin'] = true;

to have $_SESSION['loggedin'] set in an else, seems silly but I like things like that to very very explicit and not a coding error allow a drop through were people get logged in automatically.

lZakl

1:58 pm on Jun 9, 2005 (gmt 0)

10+ Year Member



About the only security note I would make is to use require ('include.inc');

I might even say require_once ...

Nice! Just the sort of tips I was looking for!

jatar_k,

You mention setting $_SESSION['loggedin'] in an else. Would it be benificial to include my HTML in that same else?

Thanks for your input guys!

-- Zak

jatar_k

5:05 am on Jun 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> include my HTML in that same else

well, I doubt it, more often than not you are redirecting them to logout if not set and then sending them somewhere else if it is.

Not sure really, depends on the logic you decide on, it isn't really cut and dried.