我需要什么
- 我需要在用户点击注销时删除 cookie。
- 代码在 chrome 和 mozilla 中运行。
- mozilla ver 30 中的错误,即 10。
php代码
public function flushAction() {
$service = $this->get('acme.twig.acme_extension');
$detect=$service->DomainDetect();
if ($this->get('session')->get('user'))
{
$session = $this->get('session');
$ses_vars = $session->all();
foreach ($ses_vars as $key => $value) {
$session->remove($key);
}
}
if(isset($_COOKIE['user']))
{
echo $_COOKIE['user'];
echo "<pre>..........";
unset($_COOKIE['user']);
setcookie("user","",time() - 3000,"/",".10times.com");
}
if(isset($_COOKIE['user_flag']))
{
echo $_COOKIE['user_flag'];
echo "<pre>..........";
unset($_COOKIE['user_flag']);
setcookie("user_flag",'',time() - 3000,"/",".10times.com");
}
if(isset($_COOKIE['email']))
{
echo $_COOKIE['email'];
echo "<pre>....";
unset($_COOKIE['email']);
setcookie("email",'',time() - 3000,"/",".10times.com");
}
if(isset($_COOKIE['name']))
{
echo $_COOKIE['name'];
echo "<pre>.........";
unset($_COOKIE['name']);
setcookie("name",'',time() - 3000,"/",".10times.com");
}
if(isset($_COOKIE['id']))
{
echo $_COOKIE['id'];
echo "<pre>.........";
unset($_COOKIE['id']);
setcookie("id",'',time() - 3000,"/",".10times.com");
}
exit;
//var_dump($_SERVER['HTTP_REFERER']);
if(strstr($_SERVER['HTTP_REFERER'],$detect))
{
header("Location:".$_SERVER["HTTP_REFERER"],TRUE,301);
exit;
}
else
{
header("Location:".$detect,TRUE,301);
exit;
}
exit;
}
资源快照
调试
我想删除 cookie 和 session 。
- 欢迎提出任何建议。
- 我调用 domainname.com/user/flush。
我试过的解决方案
$name = $this->getUser()->getAttribute('user', 'default_value');
$this->getUser()->setAttribute('user', $value);
$name->getAttributeHolder()->remove('user','','user_flag');
解决方案
if ($this->get('session')->get('user')) { $this->get('session')->remove('user'); }
我想删除 user 和 user_flag 变量的 session 。
最佳答案
除了 REMEMBERME
之外,我还有其他一些 cookie,这些 cookie 应该在成功注销后删除。我发现 Symfony 已经在 security.yml
文件的 logout
键下提供了 delete_cookies
键的功能。请参阅 the official doc for v2.8 中的示例代码第 240 行。
总而言之,
security:
...
firewalls:
your_name_of_the_firewall:
...
logout:
...
delete_cookies:
your_name_of_the_cookie: { path: null, domain: null }
我无法检查旧版本,但至少它自 v2.7 以来一直存在。对我来说,文档不够清晰,我不得不检查代码。仅供引用,它注册了 CookieClearingLogoutHandler
,并且有删除 cookie 的实际代码。
关于php - 如何删除 symfony2 中的 Session 和 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27684433/