Page 1 of 1

windows_cookie/header problem

Posted: Mon Jan 19, 2004 5:55 pm
by linuxbox
Hi. i'm having an issue concerning cookies/headers and windows/php.
i'm trying to port my login system over to windows and make sure it runs ok. everything is ok except for setting/deleting cookies before i issue a
header.
here is what fails: (this is deleting the cookie if user hits logout)

Code: Select all

if ($_GET['a'] == "logout") {
	mysql_query("UPDATE k105_users SET `file`='',`ip`='',`key`='' WHERE username='$_SESSION[user]'");   //remove login key
	$delete = mysql_db_query(k105members, "DELETE FROM useronline WHERE username2='$_SESSION[user]'");
	setcookie("login", "", -1, "/", ".mydomain.com");    //delete rememberence cookie   
        //setcookie("login", "", "", "/", ".mydomain.com,0");   //delete rememberence cookie
	session_unset();
	session_destroy();                                                                //destroy session
	header("Location: $_SERVER[HTTP_REFERER]");                                       //go back to main page
}
the odd thing is, if i comment out the header portion: header("Location: $_SERVER[HTTP_REFERER]"); it works fine.

here is the part that issues the cookie upon login:

Code: Select all

if ($query[validated] == "1") {
			$wherefrom = $_SERVER['HTTP_REFERER'];
                        $ip = $_SERVER['REMOTE_ADDR'];
                        $_SESSION['user'] = $_POST['user']; //set session user
                        $_SESSION['whereto'] = $_POST['whereto'];
                        mysql_query("UPDATE k105_users SET `lastip`='$ip',`ip`='$ip' WHERE username='$_POST[user]'");
			//"remember me" box is checked
			if ($_POST['remember']=="ON") {
				$year = (60 * 60 * 24 * 365);   	//seconds in a year
				$key = md5(uniqid(microtime()));    //key for login cookie
				
				//insert in database
				mysql_query("UPDATE k105_users SET `key`='$key' WHERE username='$_POST[user]'");
				setcookie("login", $key, time() + $year, "/", ".mydomain.com");	//set rememberence cookie
			        //setcookie("login","$key",time()+$year,"","");
				//setcookie("debug","1",time()+60*60*24*30,"","");

                               }


		
                //$back = $_SERVER['HTTP_REFERER'];
                echo "";
                header( "Location:$wherefrom");  //go back to main page
		exit;
		}
once again, the cookie does NOT get set unless i remove:

Code: Select all

header( "Location:$wherefrom");
OR unless i echo the $wherefrom variable directly before header like:

Code: Select all

echo $wherefrom;
header( "Location:$wherefrom");
can anyone tell me what the deal is here because this does not happen on my linux box. all is well on the linux box.

thanks

Posted: Mon Jan 19, 2004 6:10 pm
by patnet2004
im still a php noob but my book says to delete a cookie just setcookie(cookie_name);

you have:
setcookie("login", "", -1, "/", ".mydomain.com");

umm

Posted: Mon Jan 19, 2004 6:17 pm
by linuxbox
patnet2004 wrote:im still a php noob but my book says to delete a cookie just setcookie(cookie_name);

you have:
setcookie("login", "", -1, "/", ".mydomain.com");
i'll try that but i don't think that is the issue.

still not working

Posted: Mon Jan 19, 2004 6:32 pm
by linuxbox
that was not the problem..i have no idea why it's not working

Posted: Mon Jan 19, 2004 6:38 pm
by markl999
If you're on IIS it's a known 'bug'. See the user comments at http://www.php.net/manual/en/function.setcookie.php for more info.

Posted: Mon Jan 19, 2004 6:43 pm
by patnet2004
if its erroring just when you use that header to make it go to the front page why not leave it out and redirect with javascript?

Code: Select all

<?php
echo("<script language="JavaScript" type="text/javascript">window.location='whatever';</script>");
?>

LOL

Posted: Mon Jan 19, 2004 6:45 pm
by linuxbox
markl999 wrote:If you're on IIS it's a known 'bug'. See the user comments at http://www.php.net/manual/en/function.setcookie.php for more info.
i found the answer on another site at the exact same time i heard my email ring for this post..weird day indeed.
answer is:
header("Refresh: 0; $wherefrom: blah"); /* NEW CODE*/
// header( "Location:$wherefrom"); /*ORIGINAL*/
now i wonder if header("Refresh: 0; $wherefrom: blah"); will work on linux/apache???

Another test

Posted: Thu Jan 22, 2004 6:49 pm
by logicflow
Have you tried your refresh method, it works beautifully.
However, I have also tried to simply put exit(); after the header statement? i.e.

Code: Select all

header("Location:$wherefrom");
exit();
Seems to work as well, please try and let me know if this is the case for you. Thanks!

Cheers :D

Re: Another test

Posted: Fri Jan 23, 2004 7:53 am
by linuxbox
logicflow wrote:Have you tried your refresh method, it works beautifully.
However, I have also tried to simply put exit(); after the header statement? i.e.

Code: Select all

header("Location:$wherefrom");
exit();
Seems to work as well, please try and let me know if this is the case for you. Thanks!

Cheers :D
try it on a windows box with php man :)

Posted: Tue Feb 03, 2004 12:24 am
by pawankinger
The reason behind this is:

YOU CANNOT REDIRECT (using header Location:...) AFTER SENDING ANY OF THE HEADERS OR CONTENTS TO THE BROWSER/CLIENT. SO THIS IS NOT A BUG.

AND WHEN YOU USE header Refresh: this is equivalent to HTML META TAG -- HTTP-REFRESH which can refresh the client browser and sends him to the location specifed.

SO, AFTER DELETING OR SENDING A COOKIE YOU CAN SHOW YOUR HTML DIRECTLY BUT CANNOT REDIRECT FROM SERVER.

-Pawan Kinger

Posted: Tue Feb 03, 2004 2:42 am
by twigletmac
No need to shout :roll:.
pawankinger minus shouting wrote:You cannot redirect (using header Location:...) after sending any of the headers or contents to the browser/client. So this is not a bug.
Actually it is an IIS bug - if you set a cookie and then try and do a redirect it won't set the cookie - it's not about sending contents to the browser and it works in Apache.

Mac