Mark
Mark

Reputation: 3118

php cookie, how do I update the value?

I have some simple PHP code:

setcookie('fontSize',28, time() + 60*60*24*30, '/');

This works great, and the cookie is set, the value of 28 can be retrieved, etc. But if I change the value from 28 to, say, 48, save the file, and refresh the page, the value doesn't update to 48, but stays 28.

I suppose I could put a line of code to delete the cookie, and then set it again with the setcookie() function, but that seems like a strange way to do it. Thanks!

Upvotes: 1

Views: 1499

Answers (2)

Mark
Mark

Reputation: 3118

I needed to refresh the page twice after changing the value. Details, details, details...

Upvotes: 3

Bryan Muscedere
Bryan Muscedere

Reputation: 571

Basically, in PHP, there is no function to truly update a cookie.
What I generally use to change the value of a cookie is the setcookie() function.

In your case, you just want to use setCookie('fontSize', 48, time() + 60*60*24*30, '/') to overwrite the cookie named fontSize to one with that new value. Ensure you use the '/' parameter in the setcookie(...) function to ensure you aren't creating another cookie with that same name but in a different directory.

The problem with this solution however is that you cannot keep your previous expiration date and have to assign it with a new expiration date.

Upvotes: 2

Related Questions