11

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

rinchik
  • 2,642
  • 8
  • 29
  • 46

5 Answers5

10

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.
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
  • Here is the thing. I can stick to the browser but due to the application structure it would be easier if i't would be much easier if i could that that with PHP – rinchik Mar 06 '13 at 16:20
4

If client time is wrong, setting cookie expire with time() function may not work as expected.

To unset cookie try,

setcookie('myCookie', 'blah blah', 1);

Source: A Comment in php setcookie docs

robert
  • 8,459
  • 9
  • 45
  • 70
2

It would depend on the users PC deleting the cookie after the timeout. Personally I wouldn't trust that. I'd set the cookie to an empty value, or set it to DELETED then in your test code check if it is set and then check if the value is not blank or not DELETED

gpdaniels
  • 453
  • 1
  • 8
  • 26
1

To completely remove cookie from browser by PHP, Try this code

$name = 'exists_cookie';
unset($_COOKIE[$name]);
  // Set empty value
$blank = setcookie($name, '', time() - 3600);
IshaniNet
  • 153
  • 10
0

Can someone run up this code on a test machine I'm a tad confused as to why cookie can be "unset" but cannot be value changed or expired etc in php it seems like setcookie() isn't working at all.

<script type="text/javascript" src="jquery.js"></script>
<script src="jquery.cookie.js"></script>
<script>
   $.cookie('myCookie', '123' ,'/');
   console.log("Created myCookie");
</script>
<?php
   echo $_COOKIE['myCookie'];
   //Comment/uncomment below as required
   //setcookie("myCookie", "BLAH", time()-430000);
   //$_COOKIE['myCookie'] = "BLAH";
   setcookie('myCookie', '', 1, '/');
   echo "<br />unset myCookie";
   echo "<br />".$_COOKIE['myCookie'];
?>
<script>
console.log($.cookie('myCookie').length);
if ($.cookie('myCookie').length>0)
{
   console.log('Cookie exists  ');
   console.log($.cookie('myCookie'));
}
else
{
    console.log('Yaay! No cookie!');
}
</script>

You seem to be able to create a php <> JS cookie mismatch ie: 2 cookies called exactly the same but storing different data.

Dave
  • 3,280
  • 2
  • 22
  • 40
  • Obviously minus wasn't required though was it. – Dave Mar 06 '13 at 16:22
  • This code snippet will not work if he wants to expire the cookie. His check is in javascript, not PHP, so unsetting the variable in PHP will do nothing. – Niels Abildgaard Mar 06 '13 at 16:26
  • since cookies are clientside and php can request cookie data it should be perfectly possible to unset the php cookie and the jquery generated cookie. Having ran up a test there's an interesting thing that doing unset($_COOKIE['mycookie']) and then echoing out the contents of mycookie returns nothing but doing .length in jquery still returns a length of 3 so while the cookie is populated in in javascript still but no longer in php which theoretically shouldn't be possible oO. – Dave Mar 06 '13 at 16:34