由于是form element有了额外的装饰器 所以zend captcha变的让我无法理解 例如先$form->getelement获得元素然后->getcaptcha获得验证元素->getword()最后一步按照手册应该是获得验证码的图形 可是无论如何出不来
昨天看了3个小时手册 查了google 终于在一篇外文中找到了解决方法 思路也是很神奇的 竟然没有内置的方法 现在贴上我的login动作 在12-16行 详细的写清楚了怎么取得captcha word....enen
-
public function loginAction ()
-
{
-
if ($this->auth->hasIdentity()) {
-
$this->_redirect('/account/');
-
}
-
if ($this->getRequest()->isPost()) {
-
$authadapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
-
$authadapter->setTableName('finaluser')->setIdentityColumn('name')->setCredentialColumn('password')->setCredentialTreatment('md5(?)');
-
$f = new Zend_Filter_StripTags();
-
$username = $f->filter($this->_request->getPost('name'));
-
$password = $f->filter($this->_request->getPost('password'));
-
$captcha = $this->getRequest()->getParam('captcha');
-
$captchaId = $captcha['id'];
-
$captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captchaId);
-
$captchaIterator = $captchaSession->getIterator();
-
$captchaWord = $captchaIterator['word'];
-
if (strlen($username) == 0) {
-
$this->view->msg = '请输入用户名';
-
} elseif (strlen($captcha['input']) == 0) {
-
$this->view->msg = '请输入验证码';
-
} elseif ($captcha['input'] == $captchaWord) {
-
$authadapter->setIdentity($username)->setCredential($password);
-
$result = $this->auth->authenticate($authadapter);
-
if ($result->isValid()) {
-
//将auth的身份写入session stdclass类
-
$identity = $authadapter->getResultRowObject(array('id' , 'name' , 'type'));
-
$this->auth->getStorage()->write($identity);
-
//重新跳转到请求login的页面
-
$this->_redirect($this->getRequest()->getServer('HTTP_REFERER'));
-
} else {
-
$this->view->msg = '登陆失败';
-
}
-
}
-
}
-
$form = new UserForm();
-
$this->view->form = $form;
-
}
发表评论