繁体   English   中英

PHP Zend身份验证-如何告诉Zend用户已通过身份验证

[英]PHP Zend Authentication - How can I tell Zend the user is authenticated

我需要告诉Zend,用户是在特定情况下进行身份验证的,但我不想为此目的构建身份验证适配器。

我想要这样的东西:

if ($user == 'sam') {

    // this user is valid, tell Zend to authenticate
    Zend_Auth::authenticate(true);

}

换句话说,我不想将确定用户是否有效的任务委托给某个Auth Adapter。我想告诉Zend:“嘿,这个家伙和我在一起,所以设置身份验证cookie并让他使用我的网站”。

有没有办法做到这一点?

您可以通过写入Zend_Auth存储来实现:

$userData = (object)array('username' => 'Johny23');
Zend_Auth::getInstance()->getStorage()->write($userData);

当您这样做时,Zend会自动创建新的Zend_Auth_Storage_Session对象来处理会话。

然后,您可以检查:

$auth = Zend_Auth::getInstance();
if($auth->hasIdentity()){
    echo 'welcome back, ' . $auth->getIdentity()->username;
}

http://framework.zend.com/manual/1.12/zh/zend.auth.adapter.dbtable.html

检查“高级用法示例”下的最后一个代码,代码如下

    $registry       =   Zend_Registry::getInstance();
    $DB             =   $registry['DB'];
    $authAdapter    =   new Zend_Auth_Adapter_DbTable($DB,'usertable','username','password');

    $authAdapter->setIdentity($request->getParam('username'));
    $authAdapter->setCredential($request->getParam('password'));

    $select         =   $authAdapter->getDbSelect();
    $select->where('`active` = 1');
    $result         =   $authAdapter->authenticate();

    if($result->isValid()){
            //set user proper to zend instance
            $this->_redirect('/index');
    }
    else
    {
       //logout or redirect to same page
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM