0

I've done

<?php 
echo "initial values in the cart: <pre>";print_r($_COOKIE);echo "</pre>"; 
  $_COOKIE['cart']=null;
  setcookie("cart", "", time()-3600);  
 unset($_COOKIE['cart']); 
 unset($_COOKIE); 

 ?>
 <script>
 alert( "before delete         " +$.cookie("cart") ); 
 </script>  

   <script>
      $.cookie("cart")="";
      $.cookie('cart', null);
      $.cookie('cart',null,{path:'/');
     $.cookie("cart")=null;
     $.cookie('cart', '', { expires: -150 });
     jQuery.cookie("cart", '',{expires: -1,path:'/'});
     $.cookie("cart")=undefined;
     delete $.cookie("cart");
     $.cookie("cart","",); 
     $.removeCookie('cart', { path: '/' });  
     $.removeCookie("cart"); 
     alert( "just delete         " +$.cookie("cart") );
 </script> 
 <script>
 if($.cookie('cart') === null || $.cookie('cart') === "")
{ 
      alert( " cookie deleted 1         " +$.cookie("cart") );
}

else
{
     //have cookie
     alert( "have cookie         " +$.cookie("cart") );
} 

if (typeof $.cookie('cart') === 'undefined'){
 //no cookie
 alert( " cookie deleted 2        " +$.cookie("cart") );
}
 // if($.cookie('cart') === null || $.cookie('cart') === "" 
    // || $.(cookie('cart') === "null" || $.cookie('cart') === undefined))
// {
      // alert( " cart cookie deleted 3        " +$.cookie("cart") );
// }
</script>

  <script>
 alert( "after delete         " + $.cookie("cart") ); 
 </script>  
  <?php
  echo "Items after cookie in the cart is unset: <pre>";print_r($_COOKIE);echo "</pre>";  
  ?>

output is:
(1) it prints the initial values in the cart, cookie has cart variable set
(2) before delete alert
(3) have cookie alert
(4) after delete alert
(5) print_r throwing notice Undefined variable: _COOKIE

Insights:
--> primarily am setting cookie through jquery and am able to access it by output (1)
--> next i unset cookie print_r throwing says Undefined variable and with through alerts am still able to see the cookie value, I tried to delete the cookie in the jquery way yet in all alerts cookie value is shown, in the end again print_r($_COOKIE) says Undefined variable
--> i wish on page refresh output (1) i.e., print_($_COOKIE['cart']) has to say Undefined variable cart but still returning original/old cookie-cart variable.


please suggest me how to unset the cookie properly
with mozilla's cookie add-on i found path of cookie is correct as mentioned above

solved
leaving jquery aside i tried setcookie('cart', null, -1, '/'); and it has solved my issue!
Thanks!

now-r-never
  • 183
  • 2
  • 4
  • 18
  • 2
    You can't force the browser to delete the cookie file. You can, however, delete the contents of the cookie and expire it – NullPoiиteя May 29 '14 at 06:33
  • your question may be dup of http://stackoverflow.com/questions/15252394/php-cant-remove-cookie-that-was-set-by-javascript – NullPoiиteя May 29 '14 at 06:35
  • @NullPoiиteя I tried to unset & delete cookie variable with php & jquery but i failed to achieve the same! thanks for the reference but i found no solution for this issue. – now-r-never May 29 '14 at 06:39

1 Answers1

1

Things to consider:

  1. Don't touch variables in $_COOKIE global. It will not change the actual cookie.
  2. Javascript changes cookies live, for php you might need to refresh the page. It depends on how your script is built. Example: setting a cookie in php and change it in php, requires a refresh.
  3. Cookies have domain and path. You have set the path to / and domain to domain.com, that's exactly what you need to send in order to delete the cookie. Example:

setting a cookie:

setcookie("cart", "my content", time()+3600, "/", "domain.com");

unsetting the cookie:

setcookie("cart", "", time()-3600, "/", "domain.com");
machineaddict
  • 3,216
  • 8
  • 37
  • 61