This question already has an answer here:
这个问题在这里已有答案:
- Refresh image with a new one at the same url 16 answers
- 使用相同的网址刷新图像16个答案
i use smarty with codeigniter. my refresh captcha work in chrome correctly and change picture of captcha but it doesn't work in firefox and my captcha doesn't change.
我使用smarty与codeigniter。我的刷新验证码正确地在chrome中工作并更改验证码的图片,但它在Firefox中不起作用,我的验证码不会改变。
part of my view:
我的观点的一部分:
<script>
function refreshCaptcha() {
$("#captcha_code").attr('src','{$url}Login/captcha');
}
</script>
</head>
<form class="m-t" role="form" action="{$url}admin/Dashboard"
method="POST">
<img id="captcha_code" src="{$url}Login/captcha" />
<a name="submit" class="btnRefresh" onClick="refreshCaptcha();">Refresh
Captcha</a>
</form>
my controller:
我的控制器:
public function captcha()
{
$image = @imagecreatetruecolor(120, 30) or die("Cannot Initialize new GD
image stream");
$background = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
imagefill($image, 0, 0, $background);
$linecolor = imagecolorallocate($image, 0xCC, 0xCC, 0xCC);
$textcolor = imagecolorallocate($image, 0x33, 0x33, 0x33);
for($i=0; $i < 6; $i++)
{ imagesetthickness($image, rand(1,3));
imageline($image, 0, rand(0,30), 120, rand(0,30), $linecolor);}
session_start();
$digit = '';
for($x = 15; $x <= 75; $x += 20)
{ $digit .= ($num = rand(0, 9));
imagechar($image, rand(3, 5), $x, rand(2, 14), $num, $textcolor);
$_SESSION['captcha_code'] = $digit;
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
}
1 个解决方案
#1
1
The img src attribute is the same, so it may not refresh correctly or the browser using cache (I'm not sure that only Firefox won't refresh).
img src属性是相同的,因此它可能无法正确刷新或浏览器使用缓存(我不确定只有Firefox不会刷新)。
I recommend you to add some parameter to prevent cache, such as time to url
我建议你添加一些参数以防止缓存,例如url的时间
<script>
function refreshCaptcha() {
$("#captcha_code").attr('src','{$url}Login/captcha?_t='+new Date().getTime());
}
</script>
#1
1
The img src attribute is the same, so it may not refresh correctly or the browser using cache (I'm not sure that only Firefox won't refresh).
img src属性是相同的,因此它可能无法正确刷新或浏览器使用缓存(我不确定只有Firefox不会刷新)。
I recommend you to add some parameter to prevent cache, such as time to url
我建议你添加一些参数以防止缓存,例如url的时间
<script>
function refreshCaptcha() {
$("#captcha_code").attr('src','{$url}Login/captcha?_t='+new Date().getTime());
}
</script>