Alexandru R
Alexandru R

Reputation: 8823

How can I delete all cookies that the browser added during one request?

I'm trying to delete all cookies with javascript (or php) that a browser request has added to my computer, included the ones added by analytics and iframe. I've tried all solutions from stack and none worked.

For example, I open index.php that adds its own cookies, and has in the HTML document Google Analytics script (few other cookies), facebook like button (another cookie), advertising scripts (other cookies). I'll end up with the following:

How do I delete everything, either with a php script (ajax request) or javascript?

Note: cookies are from different domains, as explained above

Upvotes: 1

Views: 1171

Answers (2)

mschr
mschr

Reputation: 8641

As stated, you need human interaction through browser CTRL+SHIFT+DEL for clearing anything but your own scripting domain.. Here goes

<?php
$status = "";
foreach($_COOKIE as $k=>$v) {

    # each $k corresponds to a cookie
    # send cookie header, that tells browser to remove due to expired has passed
    # see http://php.net/manual/en/function.setcookie.php for more details
    setcookie($k, $v, time()-1000000);
$status .= "unset $k<br/>\n";
}
echo $status;
?>

Upvotes: 1

Bergi
Bergi

Reputation: 664185

You can neither read nor write (and that means delete) cookies from other domains for security reasons. This is especially true for HTTP-only cookies.

See also How can my website delete another site's cookies?

Upvotes: 4

Related Questions