cookie help...

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
rodrigorules
Forum Commoner
Posts: 35
Joined: Wed Jan 05, 2005 3:10 pm

cookie help...

Post 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);
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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....
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
ianlandsman
Forum Newbie
Posts: 24
Joined: Thu Dec 30, 2004 9:50 pm
Location: New York

Post 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']);

?>
rodrigorules
Forum Commoner
Posts: 35
Joined: Wed Jan 05, 2005 3:10 pm

Post by rodrigorules »

heh i like the 2nd solutino :D
thanks to both though
Post Reply