Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 118,921 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 2,008 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Im trying to learn $_COOKIE

 
Reply to this topicStart new topic

Im trying to learn $_COOKIE, But what im trying is NOT working (At least how im intending it to)

mattisdada
post 18 Jul, 2008 - 06:15 AM
Post #1


New D.I.C Head

*
Joined: 30 May, 2008
Posts: 36


My Contributions


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

This is what i have
CODE

<?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 tongue.gif. Still doesnt work when i exit browser. Or at least appear to.

This post has been edited by mattisdada: 18 Jul, 2008 - 06:17 AM
User is offlineProfile CardPM

Go to the top of the page


MitkOK
post 18 Jul, 2008 - 06:37 AM
Post #2


D.I.C Regular

Group Icon
Joined: 9 Aug, 2007
Posts: 300



Thanked 9 times

Dream Kudos: 250
My Contributions


This code set the cookie and it is avaible until the browser is closed.
User is offlineProfile CardPM

Go to the top of the page

mattisdada
post 18 Jul, 2008 - 07:08 AM
Post #3


New D.I.C Head

*
Joined: 30 May, 2008
Posts: 36


My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

AdaHacker
post 18 Jul, 2008 - 07:43 AM
Post #4


D.I.C Head

**
Joined: 17 Jun, 2008
Posts: 116



Thanked 15 times
My Contributions


QUOTE(mattisdada @ 18 Jul, 2008 - 08:15 AM) *

EDIT: I have tried setting it just for a long time tongue.gif. 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:
CODE

setcookie('key',$value, time() + (60*60*24*365));
User is offlineProfile CardPM

Go to the top of the page

mattisdada
post 18 Jul, 2008 - 06:48 PM
Post #5


New D.I.C Head

*
Joined: 30 May, 2008
Posts: 36


My Contributions


Yeah i did
CODE

time()+12323923094

and
CODE

time()*3423423423
User is offlineProfile CardPM

Go to the top of the page

AdaHacker
post 20 Jul, 2008 - 08:59 AM
Post #6


D.I.C Head

**
Joined: 17 Jun, 2008
Posts: 116



Thanked 15 times
My Contributions


QUOTE(mattisdada @ 18 Jul, 2008 - 08:48 PM) *

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.
User is offlineProfile CardPM

Go to the top of the page

mattisdada
post 21 Jul, 2008 - 05:03 AM
Post #7


New D.I.C Head

*
Joined: 30 May, 2008
Posts: 36


My Contributions


Ah thanks! biggrin.gif So it can only go so far?

And im right in beliving that
CODE

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 Jul, 2008 - 05:14 AM
User is offlineProfile CardPM

Go to the top of the page

AdaHacker
post 21 Jul, 2008 - 07:15 PM
Post #8


D.I.C Head

**
Joined: 17 Jun, 2008
Posts: 116



Thanked 15 times
My Contributions


QUOTE(mattisdada @ 21 Jul, 2008 - 07:03 AM) *

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.
User is offlineProfile CardPM

Go to the top of the page

mattisdada
post 22 Jul, 2008 - 02:00 AM
Post #9


New D.I.C Head

*
Joined: 30 May, 2008
Posts: 36


My Contributions


Ah thanks alot! I get it now smile.gif

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)
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/13/08 04:47AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month