Im trying to learn $_COOKIE

But what im trying is NOT working (At least how im intending it to)

Page 1 of 1

8 Replies - 737 Views - Last Post: 22 July 2008 - 02:00 AM Rate Topic: -----

#1 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Im trying to learn $_COOKIE

Post icon  Posted 18 July 2008 - 06:15 AM

Ok, im trying to learn how to set and display cookies and etc.

This is what i have
<?php
$comic = hello;
if (isset($_COOKIE["comic1"])) {} else {setcookie(comic1,$comic, 0);}

print $_COOKIE["comic1"];
?>



Now setting it to 0 should set the cookie not to go away untill cookies have been deleted, correct?

Well when i quit my browser, it still isnt set, or at least it doesnt appear to be set. Just comes up as a blank page. So am i doing something wrong? Or is it just a limitation?

Thanks in advance (And can the person that ansewers this explain to me why what theyve said has worked).

EDIT: I have tried setting it just for a long time :P. Still doesnt work when i exit browser. Or at least appear to.

This post has been edited by mattisdada: 18 July 2008 - 06:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Im trying to learn $_COOKIE

#2 MitkOK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 32
  • View blog
  • Posts: 403
  • Joined: 09-August 07

Re: Im trying to learn $_COOKIE

Posted 18 July 2008 - 06:37 AM

This code set the cookie and it is avaible until the browser is closed.
Was This Post Helpful? 0
  • +
  • -

#3 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Im trying to learn $_COOKIE

Posted 18 July 2008 - 07:08 AM

Allright, so is there anyway to be able to make it last forever, or virturally forever? So when the open up the site again the next day for example, it would still have that information set.
Was This Post Helpful? 0
  • +
  • -

#4 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Im trying to learn $_COOKIE

Posted 18 July 2008 - 07:43 AM

View Postmattisdada, on 18 Jul, 2008 - 08:15 AM, said:

EDIT: I have tried setting it just for a long time :P. Still doesnt work when i exit browser. Or at least appear to.

Define "a long time". What did you use for the expire parameter? If you just typed in a really big number, that won't work. If you check the documentation, you'll see that the expire parameter to setcookie() is a UNIX timestamp. That means that if you pass it a number below about 1.2 billion, then it's already expired, and if you give it a number over about 2.1 billion, then it's an invalid timestamp.

The correct method is to use an offset from the current time. Just calculate the number of seconds from now when the cookie should expire and add that to the current time. For example, this will set the cookie to expire a year from now:
setcookie('key',$value, time() + (60*60*24*365));


Was This Post Helpful? 0
  • +
  • -

#5 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Im trying to learn $_COOKIE

Posted 18 July 2008 - 06:48 PM

Yeah i did
time()+12323923094

and
time()*3423423423

Was This Post Helpful? 0
  • +
  • -

#6 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Im trying to learn $_COOKIE

Posted 20 July 2008 - 08:59 AM

View Postmattisdada, on 18 Jul, 2008 - 08:48 PM, said:

Yeah i did

Yup, those will give you invalid timestamps alright. Just add a smaller number and it should be fine.

If you want to make the cookie last "forever", one trick you can use is to just set the cookie again the next time the user visits your page. The cookie will still have an expiration date of a year from now, or 5 years, or whatever you pick, but each visit the cookie gets reset to a year from that visit.

Or you could just be lazy and set the cookie to not expire until 2038. For practical purposes, more than 5 years is close enough to forever.
Was This Post Helpful? 0
  • +
  • -

#7 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Im trying to learn $_COOKIE

Posted 21 July 2008 - 05:03 AM

Ah thanks! :D So it can only go so far?

And im right in beliving that
time()+60*60*24*30*12*5


Is 5 years? It works, but this is defintly 5 years?

This post has been edited by mattisdada: 21 July 2008 - 05:14 AM

Was This Post Helpful? 0
  • +
  • -

#8 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Im trying to learn $_COOKIE

Posted 21 July 2008 - 07:15 PM

View Postmattisdada, on 21 Jul, 2008 - 07:03 AM, said:

It works, but this is defintly 5 years?

Yup, that's five years from now, minus about 25 days (12*30 is 360 days, not 365 days). A UNIX timestamp is a 32-bit integer defined as the number of seconds since midnight January 1, 1970 UTC. So all you need to do is multiply out the number of seconds you need. However, since it's a signed 32-bit value, it can't represent any date later than he year 2038.
Was This Post Helpful? 0
  • +
  • -

#9 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Im trying to learn $_COOKIE

Posted 22 July 2008 - 02:00 AM

Ah thanks alot! I get it now :)

So is it possible to make it a 64bit integer in the future? That would make is seriously higher (2^64 is alot larger number then 2^32)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1