我正在尝试使用 codeigniter 实现验证码功能。但它不显示验证码图像。
这是我的代码:
// Controller 文件:captcha.php
<?php
class Captcha extends CI_Controller {
var $controller = "captcha";
var $viewContent = array();
public function __construct() {
parent::__construct();
$this->load->view('header');
}
/* Function To load captcha view file */
function list_captcha() {
//加载验证码助手
$this->load->helper('captcha');
$data = array(
'img_path' => './captcha/',
'img_url' => 'localhost/demoProject/application/captcha/',
'img_width' => '150',
'img_height' => 24,
'border' => 0,
'expiration' => 7200
);
$captchaArr = create_captcha($data);
$this->viewContent['captchaArr'] = $captchaArr;
$this->viewContent['captchaWord'] = $captchaArr['word'];
//echo"<pre>"; print_r($captchaArr);exit;
$this->viewContent['refreshUrl'] = "localhost/
demoProject/" . $this->controller . "/list_" . $this->controller;
$this->load->view($this->controller, $this->viewContent);
$this->load->view('footer');
}
}
?>
//查看文件:captcha.php
<script>
$(document).ready(function(){
$("#captcha").blur(function(){
if($("#captcha").val()== '<?php echo $captchaWord; ?>'){
alert('Captcha match');
}else{
alert('Please enter same word like captcha image.');
}
});
});
</script>
<table>
<tr>
<td>
Captcha Image
</td>
<td style="text-align: center">
<?php echo $captchaArr['image']; ?><br>
</td>
</tr>
<tr>
<td>Enter the same code here </td>
<td style="text-align: center">
<?php
$data = array('name' => 'captcha', 'id' => 'captcha');
echo form_input($data);
?>
</td>
</tr>
<tr>
<td>Can't read the image?</td>
<td style="text-align: center"> <a href="<?php echo $refreshUrl; ?>">Refresh</a></td>
</tr>
</table>
我不明白这段代码有什么问题......
最佳答案
我相信你的问题是这一行:
img_url' => 'localhost/demoProject/application/captcha/',
您的网址缺少 http://
协议(protocol)标识符。
img_url
从未被 CAPTCHA 帮助程序修改 - 它直接用于创建 <img>
标签,它会直接转储到您的 HTML 输出中。
当浏览器尝试加载图像时,它没有意识到这是一个网址,因此加载失败。
您应该修改这行代码以包含 http://
部分。更好的是,您应该使用 CodeIgniter 的 base_url()
生成 URL函数,以便您的代码能够灵活应对 future 的变化。
关于php - 使用 codeigniter 的验证码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14333323/