Zend_Auth部分代码示例

protected $_auth;
    protected $_userId;
    protected $_userName;

    public function init()
    {
        $this->_auth = Zend_Auth::getInstance()
             ->setStorage(new Zend_Auth_Storage_Session(‘authNameSpace’);
    }

    public function indexAction()
    {
        if ($this->_auth->hasIdentity()) {
                $this->_userId = this->_auth->getStorage()->read()->USER_ID;
                $this->_userName = $this->_auth->getStorage()->read()->USER_NAME;
            echo ‘登陆成功’;
        } else {
            $this->_forward(‘login’);
        }
    }

    protected function _logout()
    {
        $this->_auth->clearIdentity();
        Zend_Session::forgetMe();
    }

    public function loginAction()
    {
        if ($this->isPost()) {
            $f = new Zend_Filter_StripTags();
            $username = $f->filter($this->getPost(‘username’));
            $password = $f->filter($this->getPost(‘password’));

            //建立认证适配器 (DB适配器名/表名/用户名字段/密码字段)
            $authAdapter = new Zend_Auth_Adapter_DbTable($this->_db);
            $authAdapter->setTableName(‘USER’)
                        ->setIdentityColumn(‘USER_NAME’)
                        ->setCredentialColumn(‘USER_PASSWORD’)
                        ->setIdentity($username)
                        ->setCredential(md5($password));
            $result = $this->_auth->authenticate($authAdapter);

            //建立持久连接
            if ($result && $result->isValid()) {
                if ($this->getPost(‘rememberMe’)) { //记住密码
                    Zend_Session::rememberMe(‘315360000′);
                } else {
                    Zend_Session::forgetMe();
                }
                $this->_auth->getStorage()->write($authAdapter->getResultRowObject(array(‘USER_ID’, ‘USER_NAME’)));
                $this->_forward(‘index’);
            } else {
                echo ‘登陆失败’;
            }
        }
    }

One Response to “Zend_Auth部分代码示例”

  1. 衡静 Says:

    终于看见你的BLOG更新了。是不是开发没时间喔,我RSS里就你的一直没有动过。哈哈。是PHP吗,偶看不懂。

发页文章评论

Theme Brought to you by Directory Journal and Elegant Directory