SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Nov 2011
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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)

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    1: setcookie.
    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.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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.

  4. #4
    SitePoint Addict
    Join Date
    Nov 2011
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:


    PHP Code:
    setcookie('mycookie'falsetime() -3600); // old cookie removal
    setcookie('mycookie''newvalue'time()+3600); 
    or just:


    PHP Code:
    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?

    Example:

    PHP Code:
    setcookie('mycookie'falsetime() -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?

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    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.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You do not need to delete the 1st one. Forget about it, just create a new one.

  8. #8
    SitePoint Addict
    Join Date
    Nov 2011
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •