7
How can I delete COOKIES in PHP? I know that for sessions you have the session_destroy()
but for Cookies how do I?
7
How can I delete COOKIES in PHP? I know that for sessions you have the session_destroy()
but for Cookies how do I?
12
Use the function itself setcookie()
but with negative value in the third parameter (which is the expires), you can also use unset()
with the variable $_COOKIE
to delete the key in the current execution:
/**
* Unset cookies
*
* @param string $key Nome do cookie
* @param string $path (Opcional) Se definido irá remover o cookie de caminhos especificos
* @param string $domain (Opcional) Se definido irá remover o cookie de (sub)dominios especificos
* @param bool $secure (Opcional) Se definido irá remover o cookie em conexão segura (isto varia conforme o navegador)
* @return bool
*/
function unsetcookie($key, $path = '', $domain = '', $secure = false)
{
if (array_key_exists($key, $_COOKIE)) {
if (false === setcookie($key, null, -1, $path, $domain, $secure)) {
return false;
}
unset($_COOKIE[$key]);
}
return true;
}
//Elimina o cookie pro path atual
unsetcookie('meucookie');
//Elimina o cookie pro path raiz
unsetcookie('meucookie', '/');
//Elimina o cookie de um domínio quando estiver em um subdomínio por exemplo: bar.foo.com
unsetcookie('meucookie', '/', 'foo.com');
As in the example of documentation: http://php.net/manual/en/function.setcookie.php#103629
5
The response of @Guillhermenascimento answers the question very well, but I would like to bring an addition.
When it comes to Cookies, I don’t know if the right way would be to say "Delete Cookies". What is usually done to have the cookie "deleted" is to "invalidate it".
I say this because the most common way is to define a negative value for the parameter $expire
. This will make the browser understand that that cookie is "past its expiration date" and will not send it to the server.
Simply put, you delete a cookie like this:
setcookie('quero_apagar_isso', null, -1);
Logically, one might think that unset($_COOKIE['quero_apagar_isso'])
would work, but that’s not true.
The variable $_COOKIE
It’s like it’s read-only, because it references something coming from the client.
The only case where unset
will work with a variable Super Global is in the variable $_SESSION
.
Just reflect: If you used unset($_GET['id'])
, the parameter id
disappear from the url?. Obviously not. Similarly, the Cookie will not cease to exist just because you deleted a value that references it in PHP.
In the same way that you could only delete the parameter id
of a url through redirect, you will only be able to delete the Cookie by setting it in a way that the Browser considers invalid.
1
Do it this way:
<?php
unset($_COOKIE['nome_cookie']);
setcookie('nome_cookie', null, -1, '/');
-3
only:
setcookie('nome_cookie', null, -1, '/');
is necessary to clear cookies.
-4
Easy, my dear. The command is unset($_COOKIE['nomeDoCookie']);
Ready will help you with your problem.
That and good luck!
-1 wrong. This will not delete the Cookie, but will only delete the variable value. Cookies are treated differently than other data.
Browser other questions tagged php cookies
You are not signed in. Login or sign up in order to post.
Related: Cookies - PHP (setcookie) vs header (header )
– Guilherme Nascimento