简体   繁体   English

如何在Zend Framework 2中设置或删除cookie

[英]How to set or remove cookies in Zend Framework 2

I need to use cookie instead of session to persisitent the login. 我需要使用cookie而不是session来持久登录。 My login in and login out part as follow. 我的登录和登出部分如下。 My questions is that, the cookie 'admin' can not be cleared by the loginout action. 我的问题是,登录操作无法清除cookie“admin”。 I use firebug to check, the result is that in the response the cookie cleared, but in the request, the cookie set again. 我使用firebug进行检查,结果是在响应中cookie被清除,但在请求中,cookie再次设置。 namespace Application\\Controller; 命名空间Application \\ Controller;

    namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Form\LoginForm;
use Application\Model\Login;
use Application\Model\Auth;
use Application\Model\CodeExchange;

class IndexController extends AbstractActionController
{   
    public function indexAction() //login view
    {

       $request = $this->getRequest();

       if (isset($request->getCookie()->admin))
       {
          list($uid,$username,$grade,$authstring) = explode("\t",new CodeExchange($request->getCookie()->admin,'DECODE'));
          if ($uid) return $this->redirect()->toRoute('application',array('module' => 'application', 'controller' => 'index', 'action' => 'home'));
       }

       $form = new LoginForm();

       if ($request->isPost())
       {
          $login = new Login();
          $form->setInputFilter($login->getInputFilter());
          $form->setData($request->getPost());

          if ($form->isValid())
          {
             $sm = $this->getServiceLocator();
             $dbadapter = $sm->get('Zend\Db\Adapter\Adapter');
             $auth = new Auth($dbadapter,$request->getPost()->get('username'),$request->getPost()->get('passwd'));
             if ($auth->result->isValid())
             {
                $this->getResponse()->getHeaders()->addHeader(new \Zend\Http\Header\SetCookie("admin", new CodeExchange($auth->feedback->id . "\t" . $auth->feedback->username . "\t" . $auth->feedback->grade . "\t" . $auth->feedback->authrange,'ENCODE'), time()+86400));
                return $this->redirect()->toRoute('application',array('module'=>'application','controller'=>'index','action'=>'home'));
             }
          }
       }

        return new ViewModel(array('form' => $form));
    }

    public function homeAction() // main view
    {       
       if (isset($this->getRequest()->getCookie()->admin))
       {
          list($uid,$username,$grade,$authstring) = explode("\t",new CodeExchange($this->getRequest()->getCookie()->admin,'DECODE'));
          if (!$uid) return $this->redirect()->toRoute('application',array('module' => 'application', 'controller' => 'index', 'action' => 'index'));
       }
       else
       {
          return $this->redirect()->toRoute('application',array('module' => 'application', 'controller' => 'index', 'action' => 'index'));
       }

       return new ViewModel();
    }

    public function loginoutAction()
    {
       $this->getResponse()->getHeaders()->addHeader(new \Zend\Http\Header\SetCookie("admin", 'deleted', -86400));
       return $this->redirect()->toRoute('application',array('module' => 'application', 'controller' => 'index', 'action' => 'index'));
    }
}

then I changed my code to the following, using the original php code to set cookie. 然后我将我的代码改为以下,使用原始的PHP代码来设置cookie。 but not work, no cookie setted. 但没有工作,没有设置cookie。

    namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Form\LoginForm;
use Application\Model\Login;
use Application\Model\Auth;

class IndexController extends AbstractActionController
{   
    public function indexAction() //login view
    {

       $request = $this->getRequest();

       if (isset($_COOKIE["admin"])) return $this->redirect()->toRoute('application',array('module' => 'application', 'controller' => 'index', 'action' => 'home'));

       $form = new LoginForm();

       if ($request->isPost())
       {
          $login = new Login();
          $form->setInputFilter($login->getInputFilter());
          $form->setData($request->getPost());

          if ($form->isValid())
          {
             $sm = $this->getServiceLocator();
             $dbadapter = $sm->get('Zend\Db\Adapter\Adapter');
             $auth = new Auth($dbadapter,$request->getPost()->get('username'),$request->getPost()->get('passwd'));
             if ($auth->result->isValid())
             {
                setcookie('admin','fortest',86400);
                return $this->redirect()->toRoute('application',array('module'=>'application','controller'=>'index','action'=>'home'));
             }
          }
       }

        return new ViewModel(array('form' => $form));
    }

    public function homeAction() // main view
    {       
       if (isset($_COOKIE["admin"])) return $this->redirect()->toRoute('application',array('module' => 'application', 'controller' => 'index', 'action' => 'index'));

       return new ViewModel();
    }

    public function loginoutAction()
    {
       setcookie('admin','',-86400);
       return $this->redirect()->toRoute('application',array('module' => 'application', 'controller' => 'index', 'action' => 'index'));
    }
}
//get cookie
$headCookie = $this->getRequest()->getHeaders()->get('Cookie');

if(array_key_exists('lang', get_object_vars($headCookie))){
        $lang = $headCookie->lang;
    }else{
        $lang = "zh";
//set cookie
        $cookie = new  \Zend\Http\Header\SetCookie('lang',$lang,time() + 365 * 60 * 60 * 24,'/');
        $this->getResponse()->getHeaders()->addHeader($cookie);
}

//create the cookie data //创建cookie数据

$cookie = new SetCookie('token', $tokenFromUrl); $ cookie = new SetCookie('token',$ tokenFromUrl);

//Then Get the header //然后获取标题

$response = $this->getResponse()->getHeaders(); $ response = $ this-> getResponse() - > getHeaders();

//set cookie in header //在标题中设置cookie

$response->addHeader($cookie); $响应 - >的addHeader($饼干);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM