Cookies

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
eludlow
Forum Newbie
Posts: 13
Joined: Thu Jan 22, 2004 3:48 pm

Cookies

Post by eludlow »

Am experimenting with cookies for the first time, and they're not working lol!

File 1:

Code: Select all

<?php

setcookie("test", "testValue", time()+120);

echo ("Cookie set");

?>
And file 2:

Code: Select all

<?php
echo $_COOKIE['test'];
?>

I think it's setting the cookie ok, but I don't see why it's not displaying the value.

And yes, I am trying to view it within two minutes of setting it :)

Many thanks,
Ed Ludlow
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

hope this info below will help you some more

Code: Select all

setcookie('flavor', 'chocolat',1102075200);
// OR 
setcookie('flavor', 'chocolat','','','.example.com);
// OR
setcookie('flavor','',time()-86400);

Code: Select all

//reading value from cookies

if (isset($_COOKIE['flavor'])) {
   print "you ate a $_COOKIE[flavor] cookie!";
}

/*
print names and values of all cookies send in a particular request, looop trough the $_cookie
*/
foreach ($_COOKIE as $cookie_name  => $cookie_value) {
  print "$cookie_name = $cookie _value<br>";
}
eludlow
Forum Newbie
Posts: 13
Joined: Thu Jan 22, 2004 3:48 pm

Post by eludlow »

Thanks, but it simply doesn't want to set a cookie.

isset($_COOKIE['.... simply isn't producing anything.

What could be wrong?

E
eludlow
Forum Newbie
Posts: 13
Joined: Thu Jan 22, 2004 3:48 pm

Post by eludlow »

OK, found out this works.

Code: Select all

<?php

// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");

// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
   foreach ($_COOKIE['cookie'] as $name => $value) {
       echo "$name : $value <br />\n";
   }
}

?>
But why won't a single cookie, as opposed to an array work?

E
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Do you allow yourself to have cookies set?


The info i gave you should work i just tested it onmyself and it works just fine.
testcookie
This+is+a+test+cookie
localhostlocalhost.localdomain
0
1125551360
29614590
4228328656
29614589
*
code i used

Code: Select all

<?
$string_name = "testcookie";
$string_value ="This is a test cookie";
$expiry_info = time() +120;
$string_domain = "localhost.localdomain";
setcookie($string_name, $string_value, $expiry_info, $string_domain);
?>
Did you try and look for youre cookies in youre cookie folder???



cookie should look like

administrator@localhost.locahost.com or whatever
eludlow
Forum Newbie
Posts: 13
Joined: Thu Jan 22, 2004 3:48 pm

Post by eludlow »

I can't see anything in cookies folder, but surely....

setcookie('test','test',time()+120);

should set one fine, if the array method above working?!

Ed
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

oke than do this..

you want to have this cookie set.
setcookie('test','test',time()+120);
I used

Code: Select all

<? 
$string_name = "test"; 
$string_value ="test"; 
$expiry_info = time() +120; 
setcookie($string_name, $string_value, $expiry_info); 
?>
my results
test
test
localhost/cookie/
0
3665616768
29614592
2474336768
29614592
*
cookie was located in (win2k machine )

C:\Documents and Settings\Administrator\Cookies

and named administrator@cookie[1]


my results displaying the cookie
you ate a test cookie!
with code

Code: Select all

<? 
//$string_name = "test"; 
//$string_value ="test"; 
//$expiry_info = time() +120; 
//setcookie($string_name, $string_value, $expiry_info); 

if (isset($_COOKIE['test'])) { 
   print "you ate a $_COOKIE[test] cookie!"; 
} 

?>
Set cookie time to like 5 minuuts and than see if cookie has been set.

maby youre waiting to long.. 120 seconds is gone soon enough
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

me out off to have my weekend.. so cant help you some more.. today...

have a jolly nice weekend you all..

bailar , tomar :lol:

hasta el Lunes
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

If you are ever having any problems, and want to see all the cookies currently set just use this small piece of code:

Code: Select all

<?php
echo "<pre>";
print_r($_COOKIE);
echo "</pre>";
?>
Post Reply