How to Delete All Cookies of My Website in PHP

how to delete all cookies of my website in php

PHP setcookie()

Taken from that page, this will unset all of the cookies for your domain:

// unset cookies
if (isset($_SERVER['HTTP_COOKIE'])) {
$cookies = explode(';', $_SERVER['HTTP_COOKIE']);
foreach($cookies as $cookie) {
$parts = explode('=', $cookie);
$name = trim($parts[0]);
setcookie($name, '', time()-1000);
setcookie($name, '', time()-1000, '/');
}
}

http://www.php.net/manual/en/function.setcookie.php#73484

How to delete all the cookies of the browser in php

You can't.

For obvious security reasons. You can't read (and delete) cookies that belongs to another domain. If you could, than all website would have access to all cookies in your computer.

How can I delete all of my users' cookies for my website?

In this cases that we set cookie in the client side, we can set a code in the first lines of our application and remove that coockie like this:

In JavaScript:

document.cookie = NAME + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';

Or in PHP:

if (isset($_COOKIE['NAME'])) {
unset($_COOKIE['NAME']);
setcookie('NAME', null, -1, '/');
}

With this approach all users that visit your website again its cookie expires.

You can delete all cookies in PHP. please follow how to delete all cookies of my website in php also for JavaScript How can I delete all cookies with JavaScript?

Remove a cookie

You May Try this

if (isset($_COOKIE['remember_user'])) {
unset($_COOKIE['remember_user']);
setcookie('remember_user', null, -1, '/');
return true;
} else {
return false;
}

How to delete all cookies with the same name?

No not all at once. If you want remove the specific cookies you need to use the cookie name. If you want to unset all cookies you can use this:

// unset cookies
if (isset($_SERVER['HTTP_COOKIE'])) {
$cookies = explode(';', $_SERVER['HTTP_COOKIE']);
foreach($cookies as $cookie) {
$parts = explode('=', $cookie);
$name = trim($parts[0]);
setcookie($name, '', time()-1000);
setcookie($name, '', time()-1000, '/');
}
}

This is a function that is published on http://php.net/manual/en/function.setcookie.php#Hcom73484



Related Topics



Leave a reply



Submit