Solved

cookie question

Posted on 2002-05-05
9
174 Views
Last Modified: 2008-03-06
why wont the following expire after 5 seconds ... or do I misunderstand 'expire'. (I thought it should delete its self)


setcookie('bob','hello',time()+5);

0
Comment
Question by:stevencopley
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 2

Expert Comment

by:jpoesen
ID: 6990941
-> the "time" parameter is an *absolute* value, and starts counting from the moment you set the cookie.

As a result, if you use time()+5 you get the amount of seconds since 1970 (about 1 billion seconds) + 5 sec, so you set your cookie to expire in rougly 32 years.

php manual :

time()
- Return current UNIX timestamp
- usage:int time (void)

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

In short, use
setcookie('bob','hello',5);

Good luck
jpoesnen
0
 
LVL 2

Expert Comment

by:jpoesen
ID: 6990943
I can't even write my own name :(

jpoesen
0
 

Expert Comment

by:VBTom
ID: 6991027
???

Are you going to award points?
0
 
LVL 2

Expert Comment

by:jpoesen
ID: 6993282
sure hope so :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:RQuadling
ID: 6993961
jpoesen, I don't think you are correct.

From the PHP manual ...

Some examples follow how to send cookies: Example 1. setcookie() send examples

setcookie ("TestCookie", $value);
setcookie ("TestCookie", $value,time()+3600);  /* expire in 1 hour */
setcookie ("TestCookie", $value,time()+3600, "/~rasmus/", ".utoronto.ca", 1);
 
 
The setcookie parameters are ...

setcookie ( string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])

The expire parameter requires a proper time (i.e. time()).

So, there is no reason that

setcookie('bob','hello',time()+5);

will not create a cookie that will expire in 5 seconds.


Steven,

Once you've set the cookie with an automatic timeout, what are you doing to determine that the cookie has NOT expired as expected?

Richard.
0
 
LVL 2

Expert Comment

by:jpoesen
ID: 6994026
Quite right. I used on old version of my message board to check. It was a version where I already started using session vars instead.

My mistake.

setcookie('bob','hello',time()+5);

makes a cookie that expires. But I don't think that "expires" means that the cookie is deleted, just that it is no longer valid.

Thanks for noticing, RQuadling.

jpoesen.
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 100 total points
ID: 6994043
Unfortunately, a cookie being deleted from the client's harddrive is not the same as expiring.

All PHP can do is say that it is no longer required.

Ho hum!

Richard.
0
 

Author Comment

by:stevencopley
ID: 6997598
I thought that if I tried to reference

I tried what jpoesen said but 'bob' , once it had timed out I'd get '' instead of 'hello'.

I tried what jpoesen said but its still there.

what I did was saved time() to bob and then when I checked it, I compared bob to time() to see how long since it had been accessed, since it doesn't seem to disappear.

Thanks everyone for your help.

0
 

Author Comment

by:stevencopley
ID: 6997607
I meant.

I tried what jpoesen said but 'bob' , once it had timed out I thought I'd get '' instead of 'hello'.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php email 2 24
Echo images using file system 2 29
converting numbers with php 3 26
How to loop through records  using SELET COUNT 2 12
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now