3

I set following cookie when user open the site

add_action('init', 'is_it_mobile_or_desktop', 1);
    function is_it_mobile_or_desktop(){
        if (isset($_GET['site']) && in_array($_GET['site'], array('mobile', 'desktop'))){
            setcookie( 'site', $_GET['site'], time() + 3600, COOKIEPATH, COOKIE_DOMAIN);
        }
    }

add_action('init', 'who_am_i', 2);

Now I want to unset/delete this cookie when the user closes the browser (Not the tab)

How can I do that?

Foolish Coder
  • 385
  • 5
  • 20

1 Answers1

6

Specify time = 0 or blank , when you do so, the cookie will expire as the browser is closed.

setcookie( 'site', $_GET['site'], 0, COOKIEPATH, COOKIE_DOMAIN);

The cookie will not expire if only the tab is closed and not the browser.

Vivek Tankaria
  • 1,301
  • 2
  • 15
  • 35