當我加入下面的代碼重載的captcha做工精細當我在ZEND中添加CSRF時,爲什麼captcha重新加載不起作用?
在Zend中FORM:
$this->setName("login");
$this->setMethod('post');
$this->addElement('text', 'username', array(
'filters' => array('StringTrim', 'StringToLower'),
'validators' => array(
array('StringLength', false, array(0, 50)),
),
'required' => true,
'label' => 'Username:',
));
$this->addElement('password', 'password', array(
'filters' => array('StringTrim'),
'validators' => array(
array('StringLength', false, array(0, 50)),
),
'required' => true,
'label' => 'Password:',
));
// Add a captcha
$this->addElement('captcha', 'captcha', array(
'label' => 'Please enter the 5 letters displayed below:',
'required' => true,
'captcha' => array(
'captcha' => 'Figlet',
'wordLen' => 5,
'timeout' => 300
)
));
$captcha = $this->createElement('captcha', 'captcha', array(
'required' => true,
'captcha' => array(
'captcha' => 'Image',
'font' => APPLICATION_PATH . '/../public/fonts/arial.ttf',
'fontSize' => '24',
'wordLen' => 5,
'height' => '50',
'width' => '150',
'imgDir' => APPLICATION_PATH.'/../public/captcha',
'imgUrl' => Zend_Controller_Front::getInstance()->getBaseUrl().'/captcha',
'dotNoiseLevel' => 50,
'lineNoiseLevel' => 5,
),
'description' => 'Refresh Captcha Image'
));
$captcha->setLabel('Please type the words shown:');
$captcha->removeDecorator("htmlTag")->removeDecorator('label');
$captcha->addDecorator('Errors', array('class' => 'err-username', 'style' => 'display:none'));
$captcha->addDecorator('Description', array('id' => 'refreshcaptcha'));
$this->addElement($captcha);
$this->getElement('captcha')->removeDecorator("htmlTag")->removeDecorator('label');
// And finally add some CSRF protection
/*$this->addElement('hash', 'csrf', array(
'ignore' => true,
));*/
$this->addElement('submit', 'login', array(
'required' => false,
'ignore' => true,
'label' => 'Login',
));
在PHTML:
<script type="text/javascript">
$(document).ready(function() {
$('#refreshcaptcha').click(function() {
$.ajax({
url: "<?php echo $this->url(array('controller' => 'auth', 'action' => 'refresh'), 'default', false) ?>",
dataType:'json',
success: function(data) {
$('#contactForm img').attr('src', data.src);
$('#captcha-id').attr('value', data.id);
}
});
});
});
</script>
<?php
//Default
//$this->form->setAction($this->url());
//echo $this->form;
?>
<?php
$errorsMessages = $this->form->getMessages();
//http://www.websitefactors.co.uk/zend-framework/2011/06/error-class-on-form-field-errors-using-zend-form/
?>
<?php
foreach($this->form->getMessages() as $elemName=>$messages) {
foreach($messages as $message) {
$label = $this->form->getElement($elemName)->getLabel();
echo $this->escape($label.' '.$message)."<br>" ;
}
}
?>
<div id="contactForm">
<form method="<?php echo $this->form->getMethod(); ?>" action="<?php echo $this->form->getAction(); ?>">
<?php echo $this->form->username->renderViewHelper(); ?>
<?php echo $this->form->password->renderViewHelper(); ?>
<?php echo $this->form->captcha; ?>
<?php //echo $this->form->csrf->renderViewHelper(); ?>
<?php echo $this->formSubmit('submit', 'Sign in',array('class'=>'button')); ?>
</form>
</div>
當我點擊 「刷新驗證碼圖片」 時,驗證碼圖像被替換而不刷新頁面,並且它工作正常,但是當我添加下面的CSRF(跨站請求僞造)代碼並重新加載驗證碼並提交時,登錄永遠不會成功。它給我的錯誤:「需要價值,不能爲空」或「請鍵入詞顯示:驗證碼值是錯誤」
$this->addElement('hash', 'csrf', array(
'ignore' => true,
));
致命錯誤:在非對象 - $ form-> changealbumcsrf-> initCsrfToken()上調用成員函數initCsrfToken(); //重新創建令牌並將其發回原因? – Prashant 2013-06-12 10:52:37
我的不好。我正在使用過去項目中的一些代碼。複製/粘貼是:)我已經更新我的答案,使用您的驗證碼字段而不是我的。 – elightbo 2013-06-17 16:21:41
我已經設置了這個代碼,它的工作?在控制器中:$ form = new Application_Form_Loginother(); \t \t $ captcha = $ form-> getElement('captcha') - > getCaptcha(); \t \t //使用initCsrfToken GET HASH和RE-INITIALIZE \t \t $ csrf = $ form-> getElement('token'); \t \t $ csrf-> initCsrfToken(); \t \t \t \t $ data = array(); \t \t $ responseArray ['id'] = $ captcha-> generate(); \t \t $ responseArray ['src'] = $ captcha-> getImgUrl()。 $ captcha-> getId()。 $ captcha-> getSuffix(); \t \t $ responseArray ['hash'] = $ csrf-> getValue(); \t \t // echo Zend_Json :: encode($ responseArray); \t \t \t $ this - > _ helper-> json($ responseArray); – Prashant 2013-06-20 05:51:14