p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Reading a cookie


Message #1 by "Frank Fitzgerald" <writelivelihood@m...> on Tue, 21 May 2002 18:38:32
I am trying to have a cookie set by one page and read by another. I have successfully set the cookie on
the first page with this code: 

<script language="php"> 
$cookie_name = "Wally"; 
$cookie_value = "test string!"; 
$cookie_expire = time()+14400; 
$cookie_domain = "localhost"; 
setcookie($cookie_name, $cookie_value, $cookie_expire, "/", $cookie_domain, 0); 
</script> 
<html>... 

And this code on the second page tells me that the cookie is set, but it will not display the cookie's name: 

<script language="php"> 
$cookie_name = getenv($_COOKIE); 
</script> 
<html> 
<head> 
<title>readcookie</title> 
</head> 
<body> 
<script language = "php"> 
if (isset($cookie_name)) 
{ 
print "There's a cookie on your computer, and its name is $cookie_name"; 
} 
else 
{ 
print "There is no cookie"; 
} 
</script> 
</body> 
</html> 

Can someone tell me how to get name to display? 

Thanks
Message #2 by "Nikolai Devereaux" <yomama@u...> on Tue, 21 May 2002 11:09:53 -0700
> I am trying to have a cookie set by one page and read by another.
> I have successfully set the cookie on the first page with this code:

I doubt it... copying your code onto my machine helped me figure out what's
probably wrong.

However, I didn't get any cookies set with your code.
  print_r($HTTP_COOKIE_VARS)
helped prove that.


The users comments on the php manual page for set cookie
  http://www.php.net/setcookie
say that the cookie spec mandates using two dots in the domain parameter.
Since you were using "localhost", I suspected that it was the reason your
script was failing.

As soon as I replaced "localhost" with the empty string '', it worked fine.

nik


  Return to Index