如何删除symfony2中的会话和cookie

时间:2014-12-29 06:35:13

标签: php html symfony session cookies

我需要什么

  • 我需要当用户点击退出cookie被删除时。
  • 代码正在使用chrome,mozilla。
  • mozilla ver 30中的错误,即10。

php code

     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;


  }

资源快照

  • enter image description here

    调试

  • 我想删除cookie和会话。

  • 欢迎提出任何建议。
  • 我打电话给domainname.com/user/flush。

解决方案我试过

    $name = $this->getUser()->getAttribute('user', 'default_value');
    $this->getUser()->setAttribute('user', $value);
    $name->getAttributeHolder()->remove('user','','user_flag');
  • anthor solution

     if ($this->get('session')->get('user')) 
    {
        $this->get('session')->remove('user');
    }
    
  • 我想删除用户和user_flag变量的会话。

3 个答案:

答案 0 :(得分:3)

我有一些其他cookie而不是REMEMBERME,这些cookie应该在成功注销后删除。我发现Symfony已经在delete_cookies文件的logout密钥下提供了security.yml密钥的功能。请参阅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的实际代码。

答案 1 :(得分:1)

试试这个..

//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

答案 2 :(得分:0)

最适合我的解决方案是:

$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'])
);