3 Replies - 3251 Views - Last Post: 12 August 2009 - 05:56 PM Rate Topic: -----

#1 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Unsetting server variables

Posted 12 August 2009 - 12:12 PM

I've been coding in PHP for nearly two weeks is all, so I'm still pretty much a beginner with the language. My current project (which is for my master's degree) is the first web-based application I've ever developed.

If a user logs into my system and then logs out without closing the browser, then any page that the user is authorized to see is still available as if the user never logged out. I've done a bunch of testing and found that the session variables are properly unset, but the server variables $_SERVER('PHP_AUTH_USER') and $_SERVER('PHP_AUTH_PW') don't seem to get unset, which causes the login script to simply log the user back in instead of calling up the login dialog box. Code is below. (The file loginfo.php, which is referenced but not included below, simply contains the database host, username, password, and database name information. I know it is not the problem.)

file: home.php
<?php
if(!isset($_SESSION['username']))
	include('login.php');

$fname = htmlentities($_SESSION['fname']);
$lname = htmlentities($_SESSION['lname']);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>UNH CS Alumni Database--Home</title>
</head>
<body>
<div align="center">
	<img src="unhlogo.jpg" alt="UNH Logo" /><br />
	<h2>Computer Science Alumni Database</h2>

	<a href="logout.php">Log out</a>
</div>
</body>
</html>



file: login.php
<?php
if(!isset($_SESSION))
{
		session_start();
}
session_regenerate_id();

require_once('loginfo.php');
require_once('DB.php');
$user = '';
$password = '';
if(!isset($_SESSION['username']))
{
	if(!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
	{
		header('WWW-Authenticate: Basic realm="Member Area"');
		header("HTTP/1.0 401 Unauthorized");
		echo "You must enter a username and password.";
		exit;
	}
	require_once('connect.php');
	$user = $_SERVER['PHP_AUTH_USER'];
	$password = $_SERVER['PHP_AUTH_PW'];
	if(get_magic_quotes_gpc())
	{
		stripslashes($user);
		stripslashes($password);
	}
	$user = $db->escapeSimple($user);
	$password = $db->escapeSimple($password);
	$query = "SELECT * FROM users WHERE ";
	$query .= "username='".$user."' AND password=SHA1('".$password."') LIMIT 1";
	$result = runQuery($db, $query);
	$row = $result->fetchRow(DB_FETCHMODE_ASSOC);
	if(!$row)
	{
		header('WWW-Authenticate: Basic realm="Member Area"');
		header("HTTP/1.0 401 Unauthorized");
		echo "Your username and password combination was incorrect!";
		exit;
	}
	$_SESSION['username'] = $row['userName'];
	$_SESSION['fname'] = $row['fName'];
	$_SESSION['lname'] = $row['lName'];
	$_SESSION['access'] = $row['class'];
	$user = '';
	$password = '';

	if($_SESSION['access'] != 'admin' && $_SESSION['access'] != 'user')
	{
		echo 'Account inactive. Unable to continue.<br />';
		session_destroy();
		unset($_SERVER['PHP_AUTH_USER']);
		unset($_SERVER['PHP_AUTH_PW']);
		echo 'Not logged in.';
		exit;
	}
}
?>



file: connect.php
<?php
require('loginfo.php');
$db = DB::connect("mysqli://$root:$pw@$host/$database");
if(DB::isError($db))
	die("Could not connect to the database: <br />".DB::errorMessage($db));

if(!function_exists('runQuery'))
{
	function runQuery($connection, $query)
	{
		$result = $connection->query($query);
		if(DB::isError($result))
			die("Could not query the database: <br />".$query."; ".DB::errorMessage($result));

		return $result;
	}
}
?>



file: logout.php
<?php
if(!isset($_SESSION))
{
		session_start();
}

unset($_SESSION);
unset($_SERVER['PHP_AUTH_USER']);
unset($_SERVER['PHP_AUTH_PW']);
if (session_id() != "" || isset($_COOKIE[session_name()]))
	setcookie(session_name(), session_id(), 1, '/');
	
session_unset();
session_destroy();

if(!isset($_SESSION['username']))
	header('Location: index.html');
?>



The file index.html which logout.php redirects to at the end simply contains a link to home.php.

Assuming I've correctly isolated the problem, how do I unset the relevant server variables? If I haven't then what could the problem be?

Thanks in advance for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: Unsetting server variables

#2 BenignDesign  Icon User is online

  • I cause cancer.
  • member icon




Reputation: 4650
  • View blog
  • Posts: 8,874
  • Joined: 28-September 07

Re: Unsetting server variables

Posted 12 August 2009 - 03:18 PM

There is a section of the PHP manual that discusses this and ways to force a login to appear anyway.... http://us3.php.net/features.http-auth

Hope it helps!
Was This Post Helpful? 1
  • +
  • -

#3 przemass  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 166
  • Joined: 18-July 09

Re: Unsetting server variables

Posted 12 August 2009 - 04:16 PM

Try remove this if statement
if(!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
and base only on session checking.
Was This Post Helpful? 0
  • +
  • -

#4 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Unsetting server variables

Posted 12 August 2009 - 05:56 PM

View Postprzemass, on 12 Aug, 2009 - 05:16 PM, said:

Try remove this if statement
if(!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
and base only on session checking.


Unfortunately, doing that just causes it to repeatedly bring up the login window and never log in since it never sets the session variable.

After reading through the manual page posted by BenignDesign and doing a bunch more research online, I've decided that it's too complicated (for me, anyway) to do this using HTTP authentication. I've decided to authenticate using a form submission instead. I got that working in about 15 minutes after fighting with the HTTP authentication for two days.

Thanks for your help, folks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1