1

ログインを永続化するには、セッションの代わりに Cookie を使用する必要があります。私のログインとログインの部分は次のとおりです。私の質問は、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