keeping 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
C
Forum Newbie
Posts: 14
Joined: Wed Dec 10, 2003 1:31 am

keeping cookies

Post by C »

hey all,

i am a php newb and i just figured out how to set, read, and delete cookies...im very happy. anyway, my question is this: if i set a cookie with my current code:

Code: Select all

<?php
setcookie('flavor', 'chocolate chip');
echo ("you got cookies");
?>
than it will set a cookie. but the cookie it sets only lasts for how ever long i have that browser window open for. how do i make it so that the cookie is permenant and so i can read it even if i re-launch my browser?

Thanks!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://php.net/set_cookie
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])
[...]
Parameter: expire
Description: The time the cookie expires. This is a unix timestamp so is in number of seconds since the epoch. In otherwords, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime().
Examples: time()+60*60*24*30 will set the cookie to expire in 30 days. If not set, the cookie will expire at the end of the session (when the browser closes).
C
Forum Newbie
Posts: 14
Joined: Wed Dec 10, 2003 1:31 am

Post by C »

ah, so if i wanted it to be a cookie that will stay for ever id set it so that it is plus 838383827174813193912 days or somthing....THANKS!
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Code: Select all

<?php

// Set a Cookie for 1 day.
setcookie("cookieName", "cookieValue", strtotime("+1 day", time()));

// Set a Cookie for 1 week.
setcookie("cookieName", "cookieValue", strtotime("+1 week", time()));

// Set a Cookie for 1 month.
setcookie("cookieName", "cookieValue", strtotime("+1 month", time()));

// Set a Cookie for 1 year.
setcookie("cookieName", "cookieValue", strtotime("+1 year", time()));

?>
Just a couple of examples.
Hope that helps.
Post Reply