2013-05-27 61 views
0

我需要使用Cookie而不是會話來持久登錄。我的登錄和登出部分如下。我的問題是,Cookie'admin'無法通過登錄操作清除。我用firebug來檢查,結果是在響應中cookie被清除了,但是在請求中,cookie又被設置了。 namespace Application \ Controller;如何在Zend Framework中設置或刪除Cookie 2

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')); 
    } 
} 

然後我將我的代碼更改爲以下,使用原始的PHP代碼來設置cookie。但不工作,沒有設置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')); 
    } 
} 

回答

2
//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); 
} 
0

//創建cookie數據

$餅乾=新setCookie方法( '令牌',$ tokenFromUrl);

//然後把報頭

$響應= $這 - >的GetResponse() - > getHeaders();

//在報頭中設置的Cookie

$響應 - >的addHeader($餅乾);