简体   繁体   中英

Is it possible to change the value of a cookie and leave the expiration date the same in PHP?

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?

It's not possible.

If you examine the HTTP specification (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 there are better ways) is to set another cookie to store the original expiration date...

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.

Impossible.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM