1

我需要使用 cookie 而不是 session 来持久化登录。我的登录和注销部分如下。我的问题是,登录操作无法清除 cookie 'admin'。我用firebug检查,结果是响应中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;
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'));
    }
}
4

2 回答 2

4
//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);
}
于 2014-03-28T03:40:41.473 回答
1

//create the cookie data

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

//Then Get the header

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

//set cookie in header

$response->addHeader($cookie);

于 2014-08-25T06:42:16.707 回答