【问题标题】:setting and extending Session Lifetime using Zend_Auth使用 Zend_Auth 设置和延长会话生命周期
【发布时间】:2011-04-25 19:38:25
【问题描述】:

我在我的一个项目中使用 Zend_Auth,但到目前为止还没有弄清楚如何设置会话的生命周期,或者如何延长它(假设它应该运行 5 分钟,并且应该在用户使用时重置为做一个动作),这是我的初始化代码:

        $authAdapter = new Zend_Auth_Adapter_DbTable($this->_model->pdo);
        $authAdapter->setTableName('normal_folks')
           ->setIdentityColumn('username')
           ->setCredentialColumn('password');

        $post = $this->_request->getPost();

        $authAdapter->setIdentity($post['username'])
            ->setCredential($post['password']);
        $auth = Zend_Auth::getInstance();
        $result = $auth->authenticate($authAdapter);

        if($result->isValid())
        {
            $userInfo = $authAdapter->getResultRowObject(null, 'password');
            $authStorage = $auth->getStorage();
            $authStorage->write($userInfo);

            if(strlen($post['refferer']) > 1){
                header("Location: ".$post['refferer']);
            }elseif(strlen($this->_request->getParam('ref_action')) > 1){
                Zend_Controller_Action::_forward($this->_request->getParam('ref_action'),"admin",null,null);
            }else{
                Zend_Controller_Action::_forward("index","admin",null,null);
            }
        }

这是我检查用户是否登录的方式:

                if(Zend_Auth::getInstance()->hasIdentity()){
                    echo "Woho!";
                }else{
                    die("invalid-identity");
                }

它可能就在我面前,但我就是想不通,帮忙?请?漂亮吗? :D

【问题讨论】:

    标签: php zend-framework session zend-auth lifetime


    【解决方案1】:

    身份验证状态存储在已注册的身份验证存储中。默认为Zend_Session。你可以set an expiration time to the Zend_Auth namespace,例如

    $namespace = new Zend_Session_Namespace('Zend_Auth');
    $namespace->setExpirationSeconds(300);
    

    你也可以globally configure Zend_Session通过

    Zend_Session::setOptions(array(
        'cookie_lifetime' => 300,
        'gc_maxlifetime'  => 300));
    

    【讨论】:

    • 还有什么我可以在行动的情况下“刷新”那个生命周期?
    • @Hannes 我认为到期时间会随着每个请求自动刷新,因此只需更新页面即可再给您 300 秒。
    • 顺便说一句。那里有小错字;)$namespace = new Zend_Session_Namespace('Zend_Auth'); 是的,你是对的,每次调用它时它都会重置,无论出于何种原因,你的第二个解决方案都不起作用(把它放在 init() 中) - 但第一个解决方案只是花花公子:D非常感谢!
    【解决方案2】:

    如果您为 zend_auth 会话使用不同的命名空间,您可以这样做:

    $auth = Zend_Auth::getInstance ();
    $auth->setStorage ( new Zend_Auth_Storage_Session ( 'user' ) );
    
    $namespace = new Zend_Session_Namespace('user');
    $namespace->setExpirationSeconds(7200); // 2 hours
    

    【讨论】:

    • 感谢您的遮阳篷,虽然我无法验证它,因为相关应用程序不再维护
    猜你喜欢
    • 2015-07-09
    • 2019-03-06
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 2021-03-03
    • 2011-09-15
    • 2012-04-07
    相关资源
    最近更新 更多