cannot retrieve cookie

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
tengue17
Forum Newbie
Posts: 8
Joined: Tue Jul 11, 2006 1:16 am

cannot retrieve cookie

Post 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??
Last edited by tengue17 on Tue Jul 11, 2006 8:44 am, edited 1 time in total.
Zoran_Dimov
Forum Newbie
Posts: 16
Joined: Wed Jun 21, 2006 6:10 pm
Location: Macedonia
Contact:

Version of PHP

Post by Zoran_Dimov »

Check your version of PHP. Some versions work $HTTP_COOKIE_VARS["username"], instead with $_COOKIE["username"];
tengue17
Forum Newbie
Posts: 8
Joined: Tue Jul 11, 2006 1:16 am

php4 and php5

Post 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.. :(
tengue17
Forum Newbie
Posts: 8
Joined: Tue Jul 11, 2006 1:16 am

bump

Post by tengue17 »

Anyone knows a hint? please?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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);
?>
Post Reply