【问题标题】:How to Delete The Session and cookie in symfony2如何在 symfony2 中删除 Session 和 cookie
【发布时间】:2015-02-25 09:03:36
【问题描述】:

我需要什么

  • 我需要在用户点击注销时删除 cookie。
  • 代码在 chrome 和 mozilla 中运行。
  • Mozilla 版本 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 和会话。

  • 欢迎提出任何建议。
  • 我打电话给 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_flag 变量的会话。

【问题讨论】:

    标签: php html symfony session cookies


    【解决方案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

    【讨论】:

    • 我试过 if ($this->get('session')->get('user')) { $this->get('session')->remove('user '); } .. 但仍然没有终止会话 cookie 被删除
    • @user3785613 你试试“ session_destroy();”对于会话和 cookie "setcookie($name, null, time() - 3600, $path);"
    • 我已经尝试并更新了快照我想要清除用户会话但它不起作用
    • 您使用删除会话变量“$session->remove($key);”但它只删除密钥尝试“$session->remove($ses_vars[$key]);”
    • 我也试过这个..但问题仍然没有解决..清除会话很简单..但我发现我错了
    【解决方案2】:

    除了REMEMBERME 之外,我还有其他一些 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,并且有删除cookies的实际代码。

    【讨论】:

      【解决方案3】:

      对我有用的最简单的解决方案是:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-18
        • 2015-04-27
        • 2013-12-05
        • 1970-01-01
        • 1970-01-01
        • 2013-07-22
        • 2017-02-09
        • 1970-01-01
        相关资源
        最近更新 更多