Page 1 of 1

cookie help...

Posted: Wed Jan 05, 2005 5:45 pm
by rodrigorules
there no way to bind these cookies together?
( learning fast :D)

Code: Select all

<?php

setcookie("uname", $name, time()+36000);
setcookie("uage", $age, time()+36000);
?>

Posted: Wed Jan 05, 2005 5:59 pm
by pickle
What do you mean by bind? Do you want to store the name and age in 1 cookie? If so, you could just do this:

Code: Select all

$curr_time = time();
setcookie("ustats",$name.'||'.$age,$curr_time+36000);

//then later...

list($name,$age) = explode('||',$_COOKIE['ustats']);
It's also a good idea to not call time() twice, but rather call it once at the beginning of a script. That way, there's no way each cookie can be set in a different second/minute/hour/day/month/year....

Posted: Wed Jan 05, 2005 7:31 pm
by ianlandsman
You could also do:

Code: Select all

<?php
setcookie("person", serialize(array($name,$age)), time()+36000);
?>
Later get the value back by:

Code: Select all

<?php

$person = unserialize($_COOKIE['person']);

?>

Posted: Wed Jan 05, 2005 7:32 pm
by rodrigorules
heh i like the 2nd solutino :D
thanks to both though