简体   繁体   English

php:如何正确删除所有cookie?

[英]php: how to correctly remove all cookies?

Ideally, to delete cookie in php, one should set exactly the same parameters when the cookie was created, except value and expire time: 理想情况下,要删除php中的cookie,应该在创建cookie时设置完全相同的参数,除了值和过期时间:

Creating cookie: 创建cookie:

setcookie('cookie1', 'value1', time()+10000, '/', 'domain.com', false, true);

Delete cookie: 删除cookie:

setcookie('cookie1', '', time()-10000, '/', 'domain.com', false, true);

But, how can I get parameters that was set creating cookie when I want to delete it? 但是, 当我想删除它时如何获取设置创建cookie的参数? assuming that I'm not tracking these params, for example, cookies was created with some third party libraries 例如,假设我没有跟踪这些参数,就会使用某些第三方库创建cookie

Yes, there is method to delete: 是的,有删除方法:

setcookie($name, '', time() - 1000, "/");
setcookie($name, '', time() - 1000);

But is it "ideally" correct approach? 但这是“理想”正确的方法吗? do all browsers (including old) support this? 所有浏览器(包括旧版)都支持这个吗?

Any way, what is the method to correctly delete all cookies for sure for all browsers 无论如何,为所有浏览器确保正确删除所有cookie的方法是什么

How can I set exactly the same params deleting cookies (If I don't track cookies creation)? 如何设置完全相同的参数删除cookie(如果我不跟踪cookie创建)?

if (isset($_SERVER['HTTP_COOKIE'])) {//do we have any
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);//get all cookies 
    foreach($cookies as $cookie) {//loop
        $parts = explode('=', $cookie);//get the bits we need
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);//kill it
        setcookie($name, '', time()-1000, '/');//kill it more
    }
}

from the notes: http://php.net/manual/en/function.setcookie.php 来自笔记: http//php.net/manual/en/function.setcookie.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM