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
Share on other sites

I just ran your script and it correctly creates and deletes the cookie without any errors.  Could you check your php log and see what errors it's producing?  Or use the error output code below.

 

<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
?>

Link to comment
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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.