Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP can't remove cookie that was set by JavaScript

Is it possible to remove cookie that was set on front via JS with PHP?

I'm doing this:

*FRONT (JS):

if ($.cookie('myCookie'))
{
   console.log('Cookie.. :(  ');
}
else
{
    console.log('Yaay! No cookie!');
    $.cookie('myCookie', '123');
}

BACK (PHP):

if (isset($_REQUEST['removeCookie']))
{
   setcookie("myCookie", "", time()-3600);
   unset($_COOKIE['myCookie']);
}

Result:

enter image description here

Seems like it's a mistery

like image 448
rinchik Avatar asked Mar 06 '13 16:03

rinchik


1 Answers

You can't force the browser to delete the cookie file. You can, however, delete the contents of the cookie and expire it. Which is exactly what you're doing with your code above. I would probably tweak it slightly:

setcookie('myCookie', '', 1, '/'); // no need to calculate one hour ago.
like image 150
Sumit Bijvani Avatar answered Oct 22 '22 11:10

Sumit Bijvani