Page 1 of 1

Cookies

Posted: Fri Jan 23, 2004 3:31 pm
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

Posted: Fri Jan 23, 2004 3:40 pm
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>";
}

Posted: Fri Jan 23, 2004 4:03 pm
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

Posted: Fri Jan 23, 2004 4:09 pm
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

Posted: Fri Jan 23, 2004 4:17 pm
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

Posted: Fri Jan 23, 2004 4:25 pm
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

Posted: Fri Jan 23, 2004 4:33 pm
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

Posted: Fri Jan 23, 2004 4:43 pm
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

Posted: Fri Jan 23, 2004 5:45 pm
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>";
?>