0

I created a cookie in PHP:

    function set_newuser_cookie() {
      if ( !is_admin() && !isset($_COOKIE['sitename_newvisitor'])) {
        setcookie( 'sitename_newvisitor', 1, time()+3600*24*365, COOKIEPATH, COOKIE_DOMAIN, false);
      }
    }
    add_action( 'init', 'set_newuser_cookie');

I want to delete this cookie in a javascript file after a click event, like so:

$('.tbf-main-logo-image').on('click', function() {
  document.cookie = 'sitename_newvisitor=;expires=Thu, 01-Jan-70 00:00:01 GMT;';
});

I have been on every stack overflow thread pertaining to this issue and I have tried: adding a path to the cookie, made a delete_cookie function, using jQuery $.removeCookie. Nothing seems to be working. One thought is that perhaps everytime my header is loading it is recreating the cookie? But no, it shouldn't because I have an if statement looking for a cookie with that name. Help!

stevenpslade
  • 310
  • 1
  • 18
  • You should not do this, as it leaves your site open to XSS atacks. Please, read [this](http://blog.codinghorror.com/protecting-your-cookies-httponly/). – Henrique Barcelos Jan 06 '16 at 02:00
  • I think this is what you are looking for. [link](http://stackoverflow.com/questions/179355/clearing-all-cookies-with-javascript) – momouu Jan 06 '16 at 02:00
  • I moved the creation of the cookie to the JS file and deletion now works. – stevenpslade Jan 06 '16 at 03:59

0 Answers0