pmicro Posted July 8, 2009 Share Posted July 8, 2009 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 More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 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 https://forums.phpfreaks.com/topic/165130-cookie-not-deleting-im-baffled/#findComment-870724 Share on other sites More sharing options...
pmicro Posted July 8, 2009 Author Share Posted July 8, 2009 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 https://forums.phpfreaks.com/topic/165130-cookie-not-deleting-im-baffled/#findComment-870734 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 The cookie will still exist, it'll just be expired. Are you actually echoing the value of the cookie when your testing it's existence, or are you looking at the cookies cached through your browser? Link to comment https://forums.phpfreaks.com/topic/165130-cookie-not-deleting-im-baffled/#findComment-870755 Share on other sites More sharing options...
pmicro Posted July 8, 2009 Author Share Posted July 8, 2009 Looking through my browser. And the expiration date doesn't change when I run the delete function. Just stays the same date it was when it was set Link to comment https://forums.phpfreaks.com/topic/165130-cookie-not-deleting-im-baffled/#findComment-870824 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 When you actually echo the results of the cookie, does anything show after it's been deleted? Link to comment https://forums.phpfreaks.com/topic/165130-cookie-not-deleting-im-baffled/#findComment-871053 Share on other sites More sharing options...
scarhand Posted July 8, 2009 Share Posted July 8, 2009 Clear your cache. You probably got your cookies stacked while developing. Clear your browser cache and it should work now. Link to comment https://forums.phpfreaks.com/topic/165130-cookie-not-deleting-im-baffled/#findComment-871165 Share on other sites More sharing options...
pmicro Posted July 8, 2009 Author Share Posted July 8, 2009 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 https://forums.phpfreaks.com/topic/165130-cookie-not-deleting-im-baffled/#findComment-871264 Share on other sites More sharing options...
corbin Posted July 8, 2009 Share Posted July 8, 2009 echo 'Delete: ' . delete_joinfree_cookie(); Unless output buffering is turned on, that will not work. Cookie headers must be sent before any non-header content. Link to comment https://forums.phpfreaks.com/topic/165130-cookie-not-deleting-im-baffled/#findComment-871272 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.