Page 1 of 1

cannot retrieve cookie

Posted: Tue Jul 11, 2006 1:58 am
by tengue17
who ate my cookie?

Hi, i'm a supernoobie in php.
I set cookie using setcookie, but when i try to retrieve, it gave me nothing.

Code: Select all

<?php
 $ret = setcookie('username', 'admin', mktime()+120, '/');
 if (!ret) {
	 echo "unable to set cookie";
 }
 else {
	 echo "cookie is set";
 }
?>
and then to check if it works, i used this code in the body of html tags

Code: Select all

<?php
print_r($HTTP_COOKIE_VARS);
print_r($_COOKIE);
?>
I also checked $_GET and $_POST they are all fine but the cookie. who took my cookie??

Version of PHP

Posted: Tue Jul 11, 2006 2:57 am
by Zoran_Dimov
Check your version of PHP. Some versions work $HTTP_COOKIE_VARS["username"], instead with $_COOKIE["username"];

php4 and php5

Posted: Tue Jul 11, 2006 4:36 am
by tengue17
I'm using php4.4.2 with apache2 on one machine, and php4.4.2 with NetFileserver on the other machine. Both under Windows2000.
I used php5 on both machine before, but didn't work. Then i rolled back to php4 on the NFS machine, and it worked within local network, but failed to work thru internet. The one with Apache2 is still not working thru both local and internet.

I also tried $HTTP_COOKIE_VARS[] instead, but it still not working. Is it the code wrong, or is it my installation wrong? To be honest, i'm also not very sure with the installation. I used cookie before on my friend's server running linux. It worked. if($_COOKIE['username']) gave me true result.

Oh, another funny thing, the server with apache2 was running Gallery2, with php5. It worked fine. No problem at all. But when i change the php into version 4, it gives errors, won't work at all.
It's been 3 weeks now of reconfiguring, reinstalling, googling, with no luck.. :(

bump

Posted: Wed Jul 12, 2006 2:50 am
by tengue17
Anyone knows a hint? please?

Posted: Wed Jul 12, 2006 3:04 am
by Benjamin
There are some samples on how to set cookies in the manual. Might want to have a quick read and give them a try.

Please see http://www.php.net/manual/en/function.setcookie.php

Code: Select all

<?php
// set cookie
$value = 'something from somewhere';
setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */

// Print an individual cookie
echo $_COOKIE["TestCookie"];

// Delete cookie
// set the expiration date to one hour ago
setcookie ("TestCookie", "", time() - 3600);
?>