How to unset a coockie when closing the browser

Asked

Viewed 285 times

1

In PHP I am creating a login coockie like this:

$data = "e".gmdate("dmY");
setcookie("elogado", $data, time() + 600, "/");

When I close the browser and reopen, will I check the coockie like this:

if(isset($_COOKIE["elogado"])){
    echo "sim";
}

It falls inside that if, ie coockie does not disappear when closing the browser. Is there any way to unset when closing the browser and/or tab?

  • The sense of the cookie is to keep it until you close the browser. use $_SESSION for this and it will be removed when you close the browser

  • 1

    If you do not set the lifetime (duration) of a cookie, it will last as long as the browser remains open. setcookie('elogado', $data);

  • @Sveen great, it worked!

  • @Leocaracciolo tested it, but it didn’t roll...

  • See the manual in the part expire http://php.net/manual/en/function.setcookie.php which says Se configurado para 0, ou omitido, o cookie irá expirar ao fim da sessao (quando o navegador fechar). Understand that closing your browser is closing all pages regardless of whether you belong to the site.

  • @Sveen, both $_SESSION how much cookie sem data de expiração will work until you close the browser. Including in your comment you state this. What maybe AP does not know is what it means fechar o navegador in such cases. You may think that you are closing the/the pages of the website that created the Session or cookie when you should actually close all the open pages of that browser.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.