+ -
当前位置:首页 → 问答吧 → ZendFramework身份验证实例

ZendFramework身份验证实例

时间:2009-09-14

来源:互联网

public function loginAction(){
/**
* ZendFramework身份验证...实例代码
* @authorHP博客-技术-资源-技术站-面向对象
*/
if ($this->getRequest()->isPost()){
//过滤字符串种的标签
$filter = array(
'name' => 'StringTrim',
'password'=>'StringTrim');

$validation = array(
'name' => array(array('StringLength',1,5)),//字符串底限长度与最高长度
'password' => array(array('StringLength',1,64)));//字符串底限长度与最高长度

$zfi = new Zend_Filter_Input($filter,$validation,$this->getRequest()->getPost());//开始验证

if ($zfi->isValid()){//若验证通过,则执行下面程序

$db = Zend_Registry::get('db');//获取db对象

$adapter = new Zend_Auth_Adapter_DbTable($db,'name','name','password');

$adapter->setIdentity($zfi->name)
->setCredential($zfi->password);

$auth = Zend_Auth::getInstance();

$result = $auth->authenticate($adapter);

if ($result->isValid()){

$auth->getStorage()->write($adapter->getResultRowObject());//保存数据,以便下一页接收改数据集
$this->getHelper('redirector')->goto('index','index');//重定向至index控制器下的index方法

} else {

$this->getHelper('FlashMessenger')
->addMessage(\"The login credentials provided are not valid\");//发送错误信息

}
} else {//若验证未通过,则执行下面程序
foreach ($zfi->getMessages() as $feild => $messages){
foreach ($messages as $message){
$this->getHelper('FlashMessenger')
->addMessage($feild.':'.$message);//发送错误信息
}
}
$this->getHelper('redirector')->goto('login');//重定向本层控制器的login方法
}
}
$this->view->messages = $this->getHelper('FlashMessenger')->getMessages();

}

作者: 孤星泪   发布时间: 2009-09-14

不错的实例

作者: 网中浪子   发布时间: 2009-09-14

看到如此好帖,怎么能不顶呢?















=700) window.open('http://www.discuz.net/images/default/sigline.gif');" onload="if(this.offsetWidth>'700')this.width='700';if(this.offsetHeight>'700')this.height='700';" >
斗破苍穹 http://www.shucheng.com/book/5/5575/

作者: 霞奕严   发布时间: 2009-09-30