Friday, June 29, 2007

PHP: Cookies

settting cookies were just so easy.. and i dont' even know why i couldn't use it.
maybe i was just refraining from it because they are not really secure...

setcookie($name, $value, $expire);

that's it....
expiration can be set by

time()+(60*60*24*7*30)

for a month

time() will print current time stamp, and you are adding number of seconds in a month.

deleting is done the same way except you set expiration to negative value or past..then the cookies will be deleted.

setcookie('test', 0, time() - 6); or whatever it indicates the past.
not sure on how long in the past so keep expiration value reasonable.


----------------
reading the cookie is like reading post values.
$_COOKIE['name'];

$value = $_COOKIE['name'];
echo $value;

just like that....too easy...
i was again got amazed but what a stubborn mind can do for us..so be open.

one more thing you can't for get is always check if the cookie isset();

No comments: