Jump to content

Cookie not deleting... I'm baffled


pmicro

Recommended Posts

I'm trying to get a cookie to delete after it's used in my code, and I can't get it to delete for the life of me. So I pulled the add/delete code out into a separate file, to see if it was something I did wrong, and if I could fix it outside of my script and get it to work, I could add it back in.

 

Alas, no luck, so I'm here to see if one of you geniuses could lend a hand :)

 

Here's the code:

 

<?php
    /**
    * @desc Sets the joinfree cookie
    * @param string $value The content you want stored in the cookie. Set to '' to delete the cookie
    */
    function set_join_cookie($value) {

            $domain = '.mydomain.com';

            $path = '/';
            
        return setcookie("joinfree1", $value, time()+604800);//, $path, $domain);
    }

    /**
    * @desc Deletes the joinfree cookie
    */
    function delete_joinfree_cookie() {

            $domain = '.mydomain.com';
         
            $path = '/';
            
        unset($_COOKIE['joinfree1']);
        return setcookie("joinfree1", false, time() - 31536000);//, $path, $domain);
    }
    
    //echo 'Add: ' . set_join_cookie('http://mydomain.com/joinfree');
    echo 'Delete: ' . delete_joinfree_cookie();
?>

 

I switched the commented line at the bottom between Add/Delete for the test. It added the cookie with no problem, but I could never get it to delete. Also, is the unset command required? I added it when I saw that as a suggestion, but see no need for it, since the $_COOKIE array never gets sent back to the browser, does it?

 

Thanks for your help!

Link to comment
https://forums.phpfreaks.com/topic/165130-cookie-not-deleting-im-baffled/
Share on other sites

I can't find any errors on the server - no logs, and adding taht code to the script doesn't do anything either. All I get is the "Delete: 1" echoed text like I'd expect, but FF3 still says joinfree1 is still in my cookies.

 

Stranger thing is that I can update a cookie and have the expiration date update, but can't change the value to '', or set an expiration date in the past...

Thanks for your help guys - the process of talking it out with you gave me the idea to instead of deleting the cookie (which I still can't do, even with clearing my cookies at browser level, and echoing after it's been deleted, it's still there) I'm simply changing the value to 0, and checking for that in my code. Shady workaround? Yes. Working? Yes :)

 

Thanks a ton!

 

I'm not going to mark this as solved, as it's not really. But since I have a workaround, it's solved in my book.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.