2017-05-10 40 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