![](/img/trans.png)
[英]Symfony2 - How to setup a session from a cookie from another project
[英]How to Delete The Session and cookie in symfony2
我需要的
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和會話。
我嘗試過的解決方案
$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_flag變量的會話。
除REMEMBERME
,我還有其他cookie,應在成功注銷后將其刪除。 而且我發現Symfony已經在security.yml
文件的logout
密鑰下提供了delete_cookies
密鑰的功能。 請參閱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的實際代碼。
嘗試這個..
//clear Session
$session->remove('user_id');
$session->clear();
//clear Cookie
public Boolean invalidate(integer $lifetime = null)
public Boolean migrate(Boolean $destroy = false, integer $lifetime = null)
請參考以下鏈接:
http://symfony.com/doc/current/components/http_foundation/sessions.html
http://api.symfony.com/2.6/Symfony/Component/HttpFoundation/Session/Session.html
對我有用的最簡單的解決方案是:
$response = new Response();
$response->headers->clearCookie(\session_name());
或更徹底地:
$params = \session_get_cookie_params();
$response->headers->clearCookie(
\session_name(), $params['path'], $params['domain'], $params['secure'], isset($params['httponly'])
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.