ZendFramework身份验证实例
public function loginAction(){
/**
* ZendFramework身份验证...实例代码
* @author:PHP博客-技术-资源-技术站-面向对象
*/
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();
}