Redirect Issue...


#1

I’m new to PHP (been using Cold Fusion for 6 years now) and I am having serious trouble with the Header(“Location: blahblah”); thing… Do you know of any special settings in the php.ini file that I may be missing? I’m using PHP 4.3.6 on Windows 2000 with IIS5.0.

Page 1 (Member_Login.php):

[php]
if($HTTP_POST_VARS[‘action’]==“login”){
if($totalRows_rsLogin==0){
//$errorMessage = “Not Active”;
header(“Location: NoAccess.php?Page=Member_Login”);exit;
mysql_free_result($rsLogin);
} else {
mysql_free_result($rsLogin);
setcookie(“UserID”,$HTTP_POST_VARS[‘username’],time() + 2592000,"/");
// Expire Date: 30
// Expire Time: 12:00:00
header(“Location: members/process.php?mem=” . $HTTP_POST_VARS[‘username’] . “”);exit;
}
}
[/php]

Page 2 (Process.php):

[php]
if(!isset($HTTP_COOKIE_VARS[‘UserID’])){
header(“Location: …/NoAccess.php?Page=Process”);exit;
}

[/php]

Here are my php.ini settings:

session.save_path=C:winnttemp
sessions.use_cookies=1
session.use_only_cookies=1
session.cookie_lifetime=0
session.cookie_path=C:winnttemp
register_globals = On

Whenever I try to set a cookie, it will not set properly. I check my Cookies folder and nothing will appear. If I remove the Redirect in the Member_Login.php file, the cookie will set properly. However, if there is a redirect, the cookie is never set and the page just redirects without recognizing the cookie.

Any help would be apprecaited.

Thanks.


#2

Note: HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER[‘HTTP_HOST’], $_SERVER[‘PHP_SELF’] and dirname() to make an absolute URI from a relative one yourself:

[php]<?php
header(“Location: http://www.example.com/”); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>
[/php]
or
[php]<?php
header(“Location: http://” . $_SERVER[‘HTTP_HOST’]
. dirname($_SERVER[‘PHP_SELF’])
. “/” . $relative_url);
?>
[/php]
or
[php]<?php
header(“Location: http://www.example.org/~trasper/NoAcces … mber_Login”);
exit;
?>
[/php]

Also, I’m sure that you’re aware that the commands after “exit” won’t be parsed, like the “mysql_free_result($rsLogin);” but the results are usually freed when the page exits anyway.