为什么我的刷新验证码在 Firefox 中不起作用?

why my refresh CAPTCHA doesn't work in firefox?

我将 smarty 与 codeigniter 一起使用。我的刷新验证码在 chrome 中正常工作并更改了验证码图片,但它在 firefox 中不起作用,我的验证码也没有改变。

我的部分观点:

<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>

我的控制器:

   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);
        }

img src属性相同,所以可能刷新不正确或者浏览器使用了缓存(我不确定只有Firefox不会刷新)。

我建议你添加一些参数来防止缓存,比如 time to url

<script>
function refreshCaptcha() {
   $("#captcha_code").attr('src','{$url}Login/captcha?_t='+new Date().getTime());
}
</script>