Forum Moderators: coopster

Message Too Old, No Replies

Permanent cookie

         

urbley

10:35 am on Nov 2, 2004 (gmt 0)

10+ Year Member



I was hoping someone could help me understand how to set a permanent cookie.

Need to knows;

1.where to put the code snippet i.e. before all outputs?
2.how to check if it exists already and not to set it again.
3.how to set it to something unique like i was going to use.
$dt = date();
$ra = $REMOTE_ADDR;
$UID = "$dt$ra";
setcookie("UID", "$UID", time()+60*60*24*30);
4.can i add this code at the top of an html page within <?php?>

Zipper

5:38 pm on Nov 2, 2004 (gmt 0)

10+ Year Member



Hi urbley, I don't think the term permanent cookie really suites.. there's never a certainity on how permanent a cookie could get.. you can always delete it anytime u want.. anyway getting to ur questions..

1. Cookies should go together with the headers, so it has to precede the output contents.

2. To check whether a cookie exists, apply the traditional isset function..
if(isset($_COOKIE['cookie_name'])){
// do whatever
}

3. If what u want to know is whether u can use "UID" as the cookie name the answer is yes, you can. and u've got it right there. just make sure u use the proper variables and concatenate the strings right.

4. you can add it to any page you like.. but remember, only php pages (.php, .php3, .phtml etc) would actually execute the code.

DaButcher

7:12 pm on Nov 2, 2004 (gmt 0)

10+ Year Member



In sessions, you have the:
session.gc_maxlifetime