0
我使用jquery和codeigniter创建catcha。我的问题是刷新验证码图像src不刷新。使用jquery和codeigniter刷新图像src
下面是创建验证码的代码
<?php
类Get_captcha延伸,用于HTML控制器 {
function Get_captcha()
{
parent::controller();
$this->load->library('session');
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
$this->load->model('tools_model');
}
function index($random = false)
{
$rand_string = $this->tools_model->RandomString(5);
$new_sess_data = array("random_number"=>$rand_string);
$this->session->set_userdata($new_sess_data);
$image = imagecreatetruecolor(148, 25);
$dir = 'fonts/';
$num = rand(1,2);
if($num==1)
{
$font = "Capture it 2.ttf"; // font style
}
else
{
$font = "Molot.otf";// font style
}
// random number 1 or 2
$num2 = rand(1,2);
if($num2==1)
{
$color = imagecolorallocate($image, 113, 193, 217);// color
}
else
{
$color = imagecolorallocate($image, 163, 197, 82);// color
}
$white = imagecolorallocate($image, 255, 255, 255); // background color white
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 18, 0, 38, 22, $color, $dir.$font, $this->session->userdata('random_number'));
header("Content-type: image/png");
imagepng($image);
}
}
代码
<img src="<?php echo site_url('get_captcha') ?>" alt="" id="captcha" /><a href="#" class="b" id="refresh-captcha">Refresh</a>
代码的jquery
$('#refresh-captcha').click(function(){
$('#captcha').attr('src','<?php echo site_url('get_captcha') ?>');
});
嗨,我试过你的代码,但没有图像将显示。当我看着萤火虫“无法加载给定的网址”,谢谢 – tirso
那么,它试图加载的URL是什么?它有效吗?你是否尝试过手动加载它? site_url()是否输出有效的完全限定的URL? – OverZealous