简体   繁体   中英

How to set or remove cookies in Zend Framework 2

I need to use cookie instead of session to persisitent the login. My login in and login out part as follow. My questions is that, the cookie 'admin' can not be cleared by the loginout action. I use firebug to check, the result is that in the response the cookie cleared, but in the request, the cookie set again. namespace 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. but not work, no cookie setted.

    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 = new SetCookie('token', $tokenFromUrl);

//Then Get the header

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

//set cookie in header

$response->addHeader($cookie);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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