2017-05-10 72 views
2

我遇到了Zend\Session\Storage\SessionArrayStorage.php的問題。我並不完全確定發生了什麼事情,因爲登錄名/身份標識工作正常,並且由於某種原因剛剛破解。我能想到的唯一的事情就是添加一個paginator路由,但是我希望修復這個錯誤,但是它沒有做任何事情。Zend Framework 2中的會話數組存儲引發警告

這裏是警告:

Warning: array_key_exists() expects parameter 2 to be array, integer given in C:\xampp\htdocs\vendor\zendframework\zendframework\library\Zend\Session\Storage\AbstractSessionArrayStorage.php on line 400 
Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\vendor\zendframework\zendframework\library\Zend\Session\Storage\AbstractSessionArrayStorage.php on line 374 

它重複這個錯誤一堆的時間(如果有幫助,這裏是錯誤的截圖 - http://imgur.com/a/do37n

我有它的方式設置爲會話服務在Module.php中設置,並使用文件LoginAuthStorage.php來處理保存,最後通過global.php中註冊的服務調用它。

以下是我認爲相關的整個代碼:

Global.php

'service_manager' => array(
      'aliases' => array(
       'Zend\Authentication\AuthenticationService' => 'pblah-auth', 
      ), 

      'invokables' => array(
       'pblah-auth' => 'Zend\Authentication\AuthenticationService', 
      ), 


     'session' => array(
      'config' => array(
       'class' => 'Zend\Session\Config\SessionConfig', 
       'options' => array(
        'name' => 'p-blah', 
      ), 
     ), 

     'storage' => 'Zend\Session\Storage\SessionArrayStorage', 

     'validators' => array(
      'Zend\Session\Validator\RemoteAddr', 
      'Zend\Session\Validator\HttpUserAgent', 
     ), 
    ), 
), 

Module.php

use Zend\Authentication\Storage; 
use Zend\Authentication\AuthenticationService; 
use Zend\Authentication\Adapter\DbTable\CredentialTreatmentAdapter as DbTableAuthAdapter; 

public function getServiceConfig() 
{ 
    return array(
     'factories' => array(
      'Application\Model\Storage\LoginAuthStorage' => function($sm) { 
       return new LoginAuthStorage(); 
      }, 

      'MemberAuthService' => function($sm) { 
        $db_adapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        $auth_adapter = new DbTableAuthAdapter($db_adapter, 'members', 'username', 'password'); 

        $auth_service = new AuthenticationService(); 
        $auth_service->setAdapter($auth_adapter); 
        $auth_service->setStorage($sm->get('Application\Model\Storage\LoginStorage')); 

        return $auth_service; 
      } 
     ), 
    ); 
} 

控制器:

namespace Application\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 

use Application\Form\LoginForm; 
use Application\Model\Filters\Login; 
use Application\Model\Storage\LoginAuthStorage; 


class MemberLoginController extends AbstractActionController 
{ 
    protected $storage; 
    protected $auth_service; 
    protected $login_service; 
    protected $mem_service; 


    public function indexAction() 
    { 
     if ($this->getAuthService()->hasIdentity()) { 
      return $this->redirect()->toUrl('/members'); 
     } 

     $form = new LoginForm(); 

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


    public function authAction() 
    { 
     $form = new LoginForm(); 

     $request = $this->getRequest(); 

     if ($request->isPost()) { 

      $login = new Login(); 

      $form->setInputFilter($login->getInputFilter()); 
      $form->setData($request->getPost()); 

      if ($form->isValid()) { 
       $login->exchangeArray($form->getData()); 

       // first make a quick password_verify check 
       if (!$this->getLoginService()->verifyPassword($login)) { 
        $this->flashMessenger()->addErrorMessage('Invalid username and/or password'); 

        return $this->redirect()->toUrl('login-failure'); 
       } 

       // check first if a session is already active 
       if (!$this->getLoginService()->checkSession($login->username)) { 
        $this->flashMessenger()->addErrorMessage("A session is already active with that username."); 
        return $this->redirect()->toUrl('login-failure'); 
       } 

       $this->getAuthService()->getAdapter() 
       ->setIdentity($login->username) 
       ->setCredential($this->getLoginService()->verifyPassword($login)['pass']); 


       $result = $this->getAuthService()->authenticate(); 

       foreach ($result->getMessages() as $message) { 
        $this->flashMessenger()->addMessage($message); 
       } 

       if ($result->isValid()) { 
        if ($login->remember_me == 1) { 
         try { 
          $this->getServiceLocator()->get('Application\Model\Storage\LoginAuthStorage')->rememberUser(1); 

          $this->getAuthService()->setStorage($this->getServiceLocator()->get('Application\Model\Storage\LoginAuthStorage')); 

          $this->getLoginService()->insertSession($login->username, 
           $this->getLoginService()->verifyPassword($login)['pass'], session_id()); 
         } catch (\Exception $e) { 
          echo $e->getMessage(); 
         } 
        } else if ($login->remember_me == 0) { 
         try { 
          $this->getServiceLocator()->get('Application\Model\Storage\LoginAuthStorage')->rememberUser(0); 

          $this->getAuthService()->setStorage($this->getServiceLocator()->get('Application\Model\Storage\LoginAuthStorage')); 

          $this->getLoginService()->insertSession($login->username, 
           $this->getLoginService()->verifyPassword($login)['pass'], session_id()); 
         } catch (\Exception $e) { 
          echo $e->getMessage(); 
         } 
        } 

        $this->getAuthService()->getStorage()->write($login->username); 

        return $this->redirect()->toUrl('/members'); 
       } else { 
        $this->flashMessenger()->addErrorMessage('Invalid username and/or password'); 

        return $this->redirect()->toUrl('login-failure'); 
       } 
      } else { 
       return new ViewModel(array('form_error' => 'Validation Error while logging in, please try again.')); 
      } 
     } 
    } 


    public function loginfailureAction() 
    { 
     return new ViewModel(array()); 
    } 


    public function getAuthService() 
    { 
     if (!$this->auth_service) { 
      $this->auth_service = $this->getServiceLocator()->get('MemberAuthService'); 
     } 

     return $this->auth_service; 
    } 


    public function getLoginService() 
    { 
     if (!$this->login_service) { 
      $this->storage = $this->getServiceLocator()->get('Application\Model\LoginModel'); 
     } 

     return $this->storage; 
    } 
} 

LoginAuthStorage

namespace Application\Model\Storage; 

use Zend\Authentication\Storage\Session; 


class LoginAuthStorage extends Session 
{ 
    /** 
    * Sets the time for the user to be remembered after login 
    * @param number $default 
    * @param number $time 
    * @return void 
    */ 
    public function rememberUser($default = 0, $time = 1209600) 
    { 
     if ($default == 1) { 
      $this->session->getManager()->rememberMe($time); 
     } else if ($default == 0) { 
      $this->session->getManager()->rememberMe(0); 
     } 
    } 


    /** 
    * Destroys the session information 
    * @return void 
    */ 
    public function forgetUser() 
    { 
     $this->session->getManager()->forgetMe(); 
    } 
} 

index.phtml(其中$this->identity()被調用)

<h4 class="w3-center"><?php echo "Welcome " . $this->identity(); ?></h4> 

和MembersController -

namespace Members\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 



class MembersController extends AbstractActionController 
{ 
    protected $profile_service; 
    protected $groups_service; 


    public function indexAction() 
    { 
     $params = $this->identity(); 

     $dir = array_diff(scandir(getcwd() . '/public/images/profile/' . $params . '/', 1), array('.', '..', 'current', '.htaccess')); 

     if (count($dir) > 0) { 
      $images = array(); 

      foreach ($dir as $value) { 
       $images[] = "<img src=\"/images/profile/$params/$value\" class=\"w3-margin-bottom w3-round w3-border\" style=\"width: 100%; height: 88px;\">"; 
      } 

      $layout = $this->layout(); 

      natsort($images); 

      $layout->setVariable('my_images', $images); 
     } 
    } 


    public function getProfileService() 
    { 
     if (!$this->profile_service) { 
      $this->profile_service = $this->getServiceLocator()->get('Members\Model\ProfileModel'); 
     } 

     return $this->profile_service; 
    } 


    public function getGroupsService() 
    { 
     if (!$this->groups_service) { 
      $this->groups_service = $this->getServiceLocator()->get('Members\Model\GroupsModel'); 
     } 

     return $this->groups_service; 
    } 
} 

如果它還能幫助,這裏是會員Module.php

namespace Members; 

use Zend\ModuleManager\Feature\AutoloaderProviderInterface; 
use Zend\Mvc\ModuleRouteListener; 
use Zend\Mvc\MvcEvent; 
use Zend\Http; 
use Members\Model\ProfileModel; 
use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway; 
use Members\Model\Filters\EditProfile; 
use Members\Model\EditProfileModel; 
use Members\Model\GroupsModel; 


class Module implements AutoloaderProviderInterface 
{ 

    public function getAutoloaderConfig() 
    { 
     return array(
      'Zend\Loader\ClassMapAutoloader' => array(
       __DIR__ . '/autoload_classmap.php', 
      ), 
      'Zend\Loader\StandardAutoloader' => array(
       'namespaces' => array(
        __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__), 
       ), 
      ), 
     ); 
    } 


    public function getConfig() 
    { 
     return include __DIR__ . '/config/module.config.php'; 
    } 


    public function onBootstrap(MvcEvent $e) 
    { 
     $eventManager  = $e->getApplication()->getEventManager(); 
     $moduleRouteListener = new ModuleRouteListener(); 
     $moduleRouteListener->attach($eventManager); 

     $eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'checkCredentials')); 
     $eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'configureLayout')); 
    } 


    public function checkCredentials(MvcEvent $e) 
    { 
     $matches = $e->getRouteMatch(); 

     if (!$matches) { 
      return $e; 
     } 

     $route = $matches->getMatchedRouteName(); 

     if (0 !== strpos($route, 'members/') && $route !== 'members') { 
      return $e; 
     } 

     $auth_service = $e->getApplication()->getServiceManager()->get('pblah-auth'); 

     if (!$auth_service->hasIdentity()) { 
      $response = $e->getResponse(); 
      $response->setStatusCode(302); 
      $response->getHeaders() 
      ->addHeaderLine('Location', $e->getRouter()->assemble([], array('name' => 'home/member-login'))); 
      $response->sendHeaders(); 
      return $response; 
     } 

     return $e; 
    } 


    public function configureLayout(MvcEvent $e) 
    { 
     if ($e->getError()) { 
      return $e; 
     } 

     $request = $e->getRequest(); 

     if (!$request instanceof Http\Request || $request->isXmlHttpRequest()) { 
      return $e; 
     } 

     $matches = $e->getRouteMatch(); 

     if (!$matches) { 
      return $e; 
     } 

     $app = $e->getParam('application'); 
     $layout = $app->getMvcEvent()->getViewModel(); 

     $controller = $matches->getParam('controller'); 

     $module = strtolower(explode('\\', $controller)[0]); 

     if ('members' === $module) { 
      $layout->setTemplate('layout/members'); 
     } 
    } 


    public function getServiceConfig() 
    { 
     return array(
      'factories' => array(
       'Members\Module\EditProfileModel' => function ($sm) { 
        $table_gateway = $sm->get('EditProfileService'); 
        $profile = new EditProfileModel($table_gateway); 
        return $profile; 
       }, 

       'EditProfileService' => function ($sm) { 
        $db_adapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        $result_set_prototype = new ResultSet(); 
        $result_set_prototype->setArrayObjectPrototype(new EditProfile()); 
        return new TableGateway('profiles', $db_adapter, null, $result_set_prototype); 
       }, 

       'Members\Model\ProfileModel' => function ($sm) { 
        $table_gateway = $sm->get('ProfileService'); 
        $profile = new ProfileModel($table_gateway, $sm->get('pblah-auth')->getIdentity()); 

        return $profile; 
       }, 

       'ProfileService' => function ($sm) { 
        $db_adapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        return new TableGateway('profiles', $db_adapter); 
       }, 

       'Members\Model\GroupsModel' => function ($sm) { 
        $table_gateway = $sm->get('GroupsService'); 
        $group_model = new GroupsModel($table_gateway, $sm->get('pblah-auth')->getIdentity()); 

        return $group_model; 
       }, 

       'GroupsService' => function ($sm) { 
        $db_adapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        return new TableGateway('groups', $db_adapter); 
       } 
      ), 
     ); 
    } 
} 

我做一個檢查$this->identity()以查看它是否正在工作,並且它正在傳遞用戶名,但是再次發生,這兩個警告只是多次顯示在頁面上。

如需更多信息,我可以嘗試發佈更多信息。

+0

當用''pblah-auth'=>'Zend \ ServiceManager \ Factory \ InvokableFactory''替換''pblah-auth'=>'Zend \ Authentication \ AuthenticationService''時會發生什麼? (假設你使用的是最新版本的框架) –

+0

@ User210411你有沒有找到這個解決方案? – Blair

回答

0

它看起來好像在某種程度上會話全局變量包含整數而不是數組$_SESSION['__ZF']

什麼,你可以嘗試做的是轉儲$_SESSION變量這樣,看看什麼是真正的在它:

\Zend\Debug\Debug::dump($_SESSION); 

接下來,嘗試清除您的會話,然後查看記錄後再次發生了什麼

+0

它說它是空的。我已經註銷並返回,但仍在執行。 – user2101411

+0

然後在應用程序開始執行任何使用常規'var_dump'之前嘗試轉儲它。另外,你有沒有嘗試從瀏覽器清除會話或在PHP中取消設置? –

+1

是的,我清除了我的瀏覽器中的會話,並註銷調用forgetUser()和clearIdentity()之前工作正常,我不知道。 – user2101411