tags:

views:

123

answers:

3

Say I have a PHP script that creates a cookie that expires 10 days from now. Is there any way to use PHP to update the value and maintain the same expiration date?

For example, say my cookie is created today with a value of "foo" and expires on 3/13/10. Two days from now, I want to change the value to "bar". Can I still have the cookie expire on the thirteenth or am I forced to either expire the cookie immediately or extend it another 10 days?

+2  A: 

It's not possible.

If you examine the HTTP spec (or watch HTTP headers using Firebug) you'll find the browser doesn't send back the expiry date to the server, only the name and value.

A way to do this (I'm sure theres better ways) is to set another cookie to store the original expiration date...

AlexV
A: 

Well, considering it's not actually possible to fetch the expiration date of the cookie itself, I don't see how you would be able to figure out when it expires to set that expiration date again. I suppose you could set another cookie variable that contains the expiration date and then when you change cookies set the expiration date to that again, but a user could simply change it to expire in years.

animuson
A: 

Impossible. Also, it's the very bad practice to rely on the cookie expiration date. If you have certain restrictions on it's use, store them in the database, and use cookie only to identify particular user.

Col. Shrapnel