我在运行登录验证码时遇到错误。我正在使用zend框架&作为IDE的zend工作室
在使用身份验证之前未提供身份的值
Zend_Auth_Adapter_DbTable
。
以下是我写的代码:
public function authAction(){
$request = $this->getRequest();
$registry = Zend_Registry::getInstance();
$auth = Zend_Auth::getInstance();
$DB = $registry['zenddb']; //zenddb is database name
$authAdapter = new Zend_Auth_Adapter_DbTable($DB);
$authAdapter->setTableName('user');
$authAdapter->setIdentityColumn('user_name');
$authAdapter->setCredentialColumn('password');
// Set the input credential values
$uname = $request->getParam('user_name');
$paswd = $request->getParam('password');
$authAdapter->setIdentity($uname);
$authAdapter->setCredential(md5($paswd));
// Perform the authentication query, saving the result
$result = $auth->authenticate($authAdapter);
if($result->isValid()){
$data = $authAdapter->getResultRowObject(null,'password');
$auth->getStorage()->write($data);
$this->_redirect('userpage');
}else{
$this->_redirect('login');
}
}
答案 0 :(得分:0)
当您在表单中将凭据字段留空时,通常会发生此错误,
所以zend纠正是一个catch块,它会抛出你提到的错误。 Official Ticket
您可以通过在与空字段相关的字段上进行验证来解决此问题。 //例如'required' => true
答案 1 :(得分:0)
public function loginAction()
{
$this->_helper->layout->setLayout('loginlayout');
$request = $this->getRequest();
$form = new Application_Form_loginForm();
$login="";
if ($this->getRequest()->isPost()) {
if ($form->isValid($request->getPost())) {
$request = $this->getRequest();
// Set the input credential values
$registry = Zend_Registry::getInstance();
$auth = Zend_Auth::getInstance();
$params = array('host' =>'localhost',
'username' =>'root',
'password' =>'',
'dbname' =>'zendDb'
);
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
$authAdapter = new Zend_Auth_Adapter_DbTable($DB);
$authAdapter->setTableName('user');
$authAdapter->setIdentityColumn('user_name');
$authAdapter->setCredentialColumn('password');
$request = $this->getRequest();
// Set the input credential values
$uname = $request->getParam('user_name');
$paswd = $request->getParam('password');
$authAdapter->setIdentity($uname);
$authAdapter->setCredential(md5($paswd));
$auth = Zend_Auth::getInstance();
// Perform the authentication query, saving the result
$result = $auth->authenticate($authAdapter);
if($result->isValid()){
$data = $authAdapter->getResultRowObject(null,'password');
$auth->getStorage()->write($data);
$login="";
$this->_redirect('database/user');
}else{
$login="Invalid User Name or Password";
//$this->_redirect('database/login');
}
//return $this->_helper->redirector('auth');
}
}
else {
$login="";
}
$this->view->assign('title','Login');
$this->view->assign('description',$login);
$this->view->form = $form;
}
答案 2 :(得分:0)
if ($this->getRequest()->isPost()) {
if ($form->isValid($request->getPost())){
//Simply Wrap your Code between this two conditions.
}
}
正如你在这里看到的那样:
首先,你必须检查请求是否是POST。
其次,您必须检查它是否对您的表单有效。