A few questions about deleting/updating/setting cookies

A few questions about cookies:

  1. I want to update a user’s cookie. To do this, do I first need to delete the previous cookie by expiring it, or can I just use set a cookie, using the name of the cookie I want to update? I want it deleted from the user’s browser.

  2. When one deletes a cookie by expiring it, must the page be refreshed before setting a new cookie with the same name? (Background: I want to check the value of a cookie and then update it based on what he value of the cookie was. But I definitely do not want the user to have a leftover cookie or some sort of other cookie residue from the previous cookie in their browser.)

  3. Is there anyway to directly delete a cookie, or is cookie deletion done only through expiring the cookie (setting a cookie expiration time that exists in the past)

1: [FPHP]setcookie[/FPHP].
2: No, but the new cookie data wont be transmitted (and stored in the $_COOKIE array) until the next pageload (You -can- manually insert things into the $_COOKIE array, but it’s recommended you not.)
3: Only through expiry. PHP operates at the Server level; Cookie deletion is at the Client level.

PHP setcookie() is the function you can use to create, edit or delete cookie.
You can force cookie to expire by setting expire date before the current date. So the following command will expire the cookie ‘userdata’.

<?php setcookie('userdata','',(time()-3600)); ?>

When browser requests the a web page again it also sends the previously saved cookie too. Server can reads that cookie and take necessary action.

That’s what I have been doing, but it seemed a bit adhoc. Oh well.

Will a cookie with the same name overwrite a previous cookie of the same name or just replace the relevant values?

So if I want to update a cookie called “mycookie” that already exists on a user’s computer with a value should I do:

setcookie('mycookie', false, time() -3600); // old cookie removal
setcookie('mycookie', 'newvalue', time()+3600);

or just:

setcookie('mycookie', 'newvalue', time()+3600);

Secondly, I am using the values of the cookies in a database. So if I replace one cookie with a new one with a new value, and then run a query based on that value, will the query use the value of the previous cookie or the new cookie just set?


setcookie('mycookie', false, time() -3600); // cookie 1
setcookie('mycookie', 'newvalue', time()+3600); // cookie 2
// spot 1
$cookievalue = $_COOKIE['mycookie'];
$sql = "SELECT * FROM table WHERE cookie = '$cookievalue'";

Will this sql statement use cookie 1’s or cookie 2’s values? Or do I need to set a page reload at spot 1 in order to make sure $cookievalue contains the updated value?

Firstly: Just set the new value. The cookie shouldnt evaluate itself for expiry again until the next pageload (though this is on a per-browser basis, so it may vary browser-to-browser).

Secondly: You’re not setting 2 cookies there. You’re just modifying one cookie (mycookie). $_COOKIE[‘mycookie’] will contain whatever the cookie held in it -WHEN THE PAGE LOADED-. setcookie does not modify the $_COOKIE array.

Do yourself a favour and use the Developer tools that most browsers supply these days - most have good cookie analysis tools.

For FF one of my favourites add-ons is called cookie-watcher, very easy to use and sits on your bottom tool bar.

You do not need to delete the 1st one. Forget about it, just create a new one.

Thanks for the clarifications. I have been testing my cookies to see what happens. Cups, I tried the addon you mentioned but it just kept giving me a JSESSIONID=undefined message on every site I visited, including this one and my own local site.

you have to right-click it and tell it the name of the cookie you want to watch. Whether these add-ons work may depend on which version of FF you use.

I am sure that if you main browser is Chrome that there there are similar add-ons available.