Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Authentication / Redirection 1

Status
Not open for further replies.

drewson

MIS
Jun 3, 2003
50
US
I'm trying to set up a password protected section of a PHP based site. I am using the require function at the beginning of a file to include a file that checks the user's session credentials and then redirects them to an access denied page if they aren't the right user type. This check and redirect works, but it brings up the fully loaded PHP page before actually redirecting the user to the next page, so for a split second, they can see all the content on the page that's supposed to be secure.

Any ideas? Here's my code for the check/redirect file:

I know the redirect is different than the normal header(Location:), etc, but the normal function doesn't work on my hosting server.

<?php
session_start();
$MM_authorizedUsers = "admin";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;

// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && false) {
$isValid = true;
}
}
return $isValid;
}

$MM_restrictGoTo = "../login/denied.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
$MM_referrer .= "?" . $QUERY_STRING;
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header ("HTTP/1.0 302 Moved");
return header("refresh: 0;url=" . $MM_restrictGoTo);
exit;
}
?>
 
A quick observation first.

You're using:

Code:
header ("HTTP/1.0 302 Moved");
return header("refresh: 0;url=" . $MM_restrictGoTo);

This is not the recommended way of performing a redirect. Instead just perform:

Code:
header ("Location: " . $MM_restrictGoTo);

PHP will automatically set the request status code to 302 for you.

Also, this snippet:
Code:
if (($strUsers == "") && false)
{ 
	$isValid = true; 
}

can be removed any value logically ANDed with FALSE will always be FALSE, so the if-block will never be executed.


I don't quite understand your logic. If the user is authorized, he's sent to denied.php?



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Most of the code is straight from Dreamweaver's restrict access module, and I didn't go through everything myself quite yet.

The redirect that you describe isn't supported on my host.

Would my logic problem cause an issue? It seems like the redirect isn't executing before the page fully loads.
 
drewson said:
The redirect that you describe isn't supported on my host.

Your web server doesn't support the "Location:" HTTP header as defined in RFC 2616? That doesn't sound right to me.



It could very well be that your logic could be the problem. Normally, using header() after sending output will cause PHP to issue an error complaining that it has already sent content and cannot send any more headers. Your system, however, may be using PHP's output buffering system.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I didn't believe that they didn't support it either, but that's what the systems admin told me. I'm trying to just get my site transferred to a different server.

Thanks!
 
You know, what someone says the server supports and what the server actually supports are not necessarily the same thing.....



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I tend to agree, but I just tested my code with your redirect function on a different server, and the code acts properly on that server, but doesn't work on my current one.

Thanks again.
 
Here are the headers that I get back when using the header function (and it doesn't redirect):

GET HTTP/1.0
Accept: */*
Accept-Language: en-us
Pragma: no-cache
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)
Host: portal.gcrltd.com
Proxy-Connection: Keep-Alive
Cookie: PHPSESSID=f06046cad18e0ab55d89fbbb060c17eb
NovINet: v1.0

HTTP/1.0 200 OK
Date: Thu, 22 Apr 2004 16:29:59 GMT
Server: 4D_WebSTAR_S/5.2.3 (MacOS X)
Content-Length: 0
Location: ../login/denied.php?accesscheck=
Pragma: No-Cache
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Set-Cookie: PHPSESSID=f06046cad18e0ab55d89fbbb060c17eb; path=/
X-Powered-By: PHP/4.3.4
Content-Type: text/html
Status: 302

I thought it might be the WebSTAR server, but I still haven't much of a clue.
 
Yeah, I see the problem. It probably is the WebStar server.

For the "Location:" header problem to work, the HTTP reply status code must be set to 302. However, the HTTP reply from the WebStar server begins "HTTP/1.0 200 OK", which indicates the script was successfully found.

The server does later send the header "Status: 302" but browsers are looking for the 302 status in the "HTTP/1.0" line.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top