取消设置cookies php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12491756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:40:01  来源:igfitidea点击:

Unset cookies php

phpcookies

提问by michael

I have this code that setted when login check is fine:

我在登录检查正常时设置了以下代码:

if((isset($_POST["remember_me"]))&&($_POST["remember_me"]==1))
    {
    setcookie('email', $username, time()+3600);
    setcookie('pass', $pass, time()+3600);
    }

Now, when I click on logout link (logout.php) i did this:

现在,当我点击注销链接(logout.php)时,我这样做了:

<?php session_start();
setcookie("email", '', 1, "");
setcookie("pass", '', 1, "");
$_SESSION["login"] = "";
header("location: aforum/enter_furom.php");
?>

I didn't use destroy session because I don't want to destroy all sessions.... now destroying a session is working fine... but when I try to unset cookies, the browsers (all browsers: explorer, chrome, firefox, mozilla) give me an error saying that the new cookies cant be setted...any help to unset the above cookies ?

我没有使用销毁会话,因为我不想销毁所有会话......现在销毁会话工作正常......但是当我尝试取消设置 cookie 时,浏览器(所有浏览器:资源管理器、chrome、firefox , mozilla) 给我一个错误,说无法设置新的 cookie......有什么帮助可以取消设置上述 cookie 吗?

回答by Gung Foo

either use the superglobal _COOKIEvariable:

要么使用超全局_COOKIE变量:

unset($_COOKIE['mycookiename']);

or call setcookie()with onlythe cookies name

或致电setcookie()只有饼干名称

setcookie('mycookiename');

To reset your cookies at logout use:

要在注销时重置您的 cookie,请使用:

setcookie('pass');
setcookie('email');

For you login check:

对于您登录检查:

if(
  isset($_POST["remember_me"]) &&
  $_POST["remember_me"]==1  &&
  $_COOKIE['pass'] != NULL &&
  $_COOKIE['email'] != NULL &&
)

回答by xtds

setcookie('cookiename', '', time()-3600);

回答by Richard

Check in your browser for the directory where the cookie operates. And unset it by specify the path the cookie have. Like in the example if the cookie directory is /aforum/

在您的浏览器中查看 cookie 运行的目录。并通过指定 cookie 的路径来取消设置。就像在示例中一样,如果 cookie 目录是/aforum/

setcookie ("email","",time()-1,"/aforum/","http:// yourdomain.com");

回答by Nikunj

try this

尝试这个

    setcookie ("email", "", time() - 3600);
    setcookie ("pass", "", time() - 3600);

回答by Amit Merchant

Just set the value of cookie to falsein order to unset it,

只需将 cookie 的值设置false为以取消设置,

setcookie('cookiename', false);

That's the easiest way to do it.

这是最简单的方法。

回答by BenM

To unset cookies in PHP, simply set their expiry time to a time in the past. For example:

要在 PHP 中取消设置 cookie,只需将它们的到期时间设置为过去的时间。例如:

$expire = time() - 300;
setcookie("email", '', $expire);
setcookie("pass", '', $expire);

回答by Martin

You need to set your expire time to the past, e.g.

您需要将过期时间设置为过去,例如

setcookie('email', '', time()-3600);

Also you should be using an Absolute URIfor your header('Location:' ....).

你也应该Absolute URI为你的header('Location:' ....).

回答by NappingRabbit

In Chrome and IE8+ at least, the following will remove the cookie from the browser. It will not be reflected in the $_COOKIEarray until the page is reloaded however.

至少在 Chrome 和 IE8+ 中,以下内容将从浏览器中删除 cookie。但是,在$_COOKIE重新加载页面之前,它不会反映在数组中。

setcookie('cookiename','',0,'/',$cookieDomain)

setcookie('cookiename','',0,'/',$cookieDomain)

you may be able to leave off a few parameters here, but the important thing is you are setting an empty string, and that removes the cookie from the browser.

您可以在此处省略一些参数,但重要的是您正在设置一个空字符串,这会从浏览器中删除 cookie。