windows_cookie/header problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
linuxbox
Forum Newbie
Posts: 18
Joined: Fri Dec 12, 2003 5:16 pm

windows_cookie/header problem

Post 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
patnet2004
Forum Newbie
Posts: 14
Joined: Sat Jul 19, 2003 3:26 am
Location: Computer Desk

Post 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");
linuxbox
Forum Newbie
Posts: 18
Joined: Fri Dec 12, 2003 5:16 pm

umm

Post 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.
linuxbox
Forum Newbie
Posts: 18
Joined: Fri Dec 12, 2003 5:16 pm

still not working

Post by linuxbox »

that was not the problem..i have no idea why it's not working
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
patnet2004
Forum Newbie
Posts: 14
Joined: Sat Jul 19, 2003 3:26 am
Location: Computer Desk

Post 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>");
?>
linuxbox
Forum Newbie
Posts: 18
Joined: Fri Dec 12, 2003 5:16 pm

LOL

Post 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???
logicflow
Forum Newbie
Posts: 1
Joined: Thu Jan 22, 2004 6:49 pm
Location: Melbourne

Another test

Post 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
linuxbox
Forum Newbie
Posts: 18
Joined: Fri Dec 12, 2003 5:16 pm

Re: Another test

Post 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 :)
pawankinger
Forum Newbie
Posts: 1
Joined: Tue Feb 03, 2004 12:24 am

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply