我从phpcaptcha下载了securimage库,将其解压缩到system / library / securimage并完成以下操作:
的index.php:
require_once(DIR_SYSTEM . 'library/securimage/securimage.php');
$registry->set('securimage', new Securimage($registry));
目录\视图\主题\ my_theme \模板\信息\ contact.tpl:
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data">
...
<img id="captcha_img" src="index.php?route=information/contact/securimage" alt="CAPTCHA Image" />
<a href="#" onclick="document.getElementById('captcha_img').src = 'system/library/securimage/securimage_show.php?' + Math.random(); return false">
<img src="system/library/securimage/images/refresh.png" height="25" width="25" alt="Reload Image" onclick="this.blur()" align="bottom" border="0"></a>
<label for="captcha" class="required"><?php echo $entry_captcha; ?></label>
<input type="text" id="captcha" name="captcha" size="31" maxlength="6" value="" onblur="check('captcha');" />
<span class="txt"><?php echo $entry_captcha_tip; ?></span>
<br />
<?php if ($error_captcha) { ?><span class="error_txt"><?php echo $error_captcha; ?></span><?php } ?>
...
目录\控制器\信息\ contact.php:
protected function validate() {
$this->log->write('SESSION CODE: ' . $this->session->data['captcha']);// 2nd variable in log
$this->log->write('REQUEST CODE: ' . $this->request->post['captcha']);// 3rd variable in log
...
if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
$this->error['captcha'] = $this->language->get('error_captcha');
}
}
public function securimage() {
$this->session->data['captcha'] = $this->securimage->getCode();
$this->log->write('IMAGE CODE: ' . $this->session->data['captcha']); // 1st variable in log
$this->securimage->show();
}
...
现在,这是它的工作原理:
有谁可以解释发生了什么?怎么可能$ this-&gt; session-&gt; data ['captcha'];落后2步?请帮我修理一下。如何在提交之前实际获得正确的图像代码。谢谢。
[编辑] 奇怪的行为继续......(考虑记录的变量):
在我看来,首先,IMAGE CODE在第一页加载时加载两次,getCode()获取第一个代码,而页面上的图像再次加载,然后存储在会话变量中并显示作为图像代码。这只发生在第一次加载页面时。然后,在第二次提交之后,会话将获取上一次提交的值,而不是刚刚提交的值,而请求代码是正确的。
这告诉你什么?有人可以帮忙吗?图像代码和会话代码应该相同。为什么securimage()和validate()中这些值有所不同?作为REQUEST CODE = IMAGE CODE,看起来securimage()会话正确地采用了图像代码,但作为SESSION CODE!= IMAGE CODE,validate()会话从哪里获取其值?为什么它只更新下次提交的值?
[解决]
解决方案比人们想象的要简单得多。它所需要的只是:
protected function validate() {
...
if (!$this->securimage->check($this->request->post['captcha'])) {
$this->error['captcha'] = $this->language->get('error_captcha');
}
}
public function securimage() {
$this->securimage->show();
}
无需使用会话!看起来像Securimage自己处理。非常感谢drew0!