Tags
PHP
Asked 3 years ago
17 Jun 2021
Views 214
Rickie

Rickie posted

How do you update a cookie in PHP?

How do you update a cookie in PHP?
kord

kord
answered Apr 27 '23 00:00

To update a cookie in PHP, you need to use the setcookie() function with the same cookie name and new value. Here's an example:



// Set the initial cookie
setcookie('my_cookie', 'initial_value', time() + 3600);

// Update the cookie value
setcookie('my_cookie', 'new_value', time() + 3600);

In this example, the first setcookie () call sets a cookie called my_cookie with an initial value of initial_value and an expiration time of 1 hour (3600 seconds) from the current time.

To update the cookie value, you simply call the setcookie () function again with the same cookie name but a new value of new_value and the same expiration time. The browser will overwrite the existing cookie with the new value.

Note that you cannot update a cookie that has already been sent to the browser. Once a cookie has been sent, it cannot be changed until the next page load. To update a cookie, you must call the setcookie () function before any other output is sent to the browser.
Post Answer