2012-06-27 40 views
0

我写了下面的代码在PHP中生成验证码。该代码在https://下运行,在IE9,Mozilla,Chrome和Safari中运行得非常好。但是,在IE8中,它有时会起作用,有时不起作用(显示破碎的图像)。有谁知道发生了什么事?任何人都可以看到什么毛病下面的代码会导致问题IE8:captcha有时工作和东西不工作在IE8下的https://

 class Captcha_generator{ 

     function __construct() 
     { 

     } 

     public function get_image($callType) 
     { 
      $font = './image/MONOFONT.TTF'; 
      $width='140'; 
      $height='40'; 
      $font_size = $height * 0.75; 
      $angle = 3; 

      //Now lets use md5 to generate a totally random string 
      $md5 = md5(microtime() * mktime()); 

      /* 
      We dont need a 32 character long string so we trim it down to 5 
      */ 
      $string = substr($md5,0,5); 
      /* 
      Now for the GD stuff, for ease of use lets create 
      the image from a background image. 
      */ 


      $captcha = imagecreate($width, $height); 
rand(0,255)); 


      /* 
      Lets set the colours, the colour $line is used to generate lines. 
      Using a blue misty colours. The colour codes are in RGB 
      */ 

      $background_color = imagecolorallocate($captcha, 255, 255, 255); 
      $text_color = imagecolorallocate($captcha, 20, 40, 100); 
      $noise_color = imagecolorallocate($captcha, 100, 120, 180); 

      /* generate random dots in background */ 
      for($i=0; $i<($width*$height)/3; $i++) { 
      imagefilledellipse($captcha, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color); 
      } 
      /* generate random lines in background */ 
      for($i=0; $i<($width*$height)/150; $i++) { 
      imageline($captcha, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color); 
      } 



      /* 
      Encrypt and store the key inside of a session 
      */ 

      $_SESSION['captcha_key'] = md5("SDFSDTRSDERGTGGT".$string."AAAAA"); 

      /* 
      Output the image 
      */ 

      imagettftext($captcha, $font_size, $angle, 30, 30, $text_color, $font , $string); 
      imagepng($captcha, './image/captcha.png'); 


      if($callType == "internal") 
      { 
       return "<img style='z-index:90' width='130' height='35' src='image/captcha.png?".time()."' />; 
         <input type='hidden' name='powerCode' value='".$_SESSION['captcha_key']."'/>"; 
      } 
      else 
      { 
       echo "<img style='z-index:90' width='130' height='35' src='image/captcha.png?".time()."' />"; 
       echo "<input type='hidden' name='powerCode' value='".$_SESSION['captcha_key']."'/>"; 
      } 
     } 


    } 
+1

究竟是什么不工作?破碎的图像?无法验证? – drew010

+0

@ drew010:破碎的图像 –

+0

由于您正在将验证码写入同一图像(captcha.png),因此可能是图像正在写入,必须为每个人共享。我会使用'imagepng($ captcha,'./image/'。$ _SESSION ['captcha_key']。'.png');'而不是。 – drew010

回答

1

这比回答的评论,只显示如何开启验证码PNG图像到数据的URI:

 /* 
     Output the image 
     */ 

     imagettftext($captcha, $font_size, $angle, 30, 30, $text_color, $font , $string); 
     ob_start(); 
     imagepng($captcha); 
     $captchaURI= 'data:image/png;base64,'.base64_encode(ob_get_clean()); 

此处请注意输出控制功能的用法。它们通常非常有用,例如以及使您的功能回声或返回字符串而不复制代码:

 ob_start(); 
     echo "<img style='z-index:90' width='130' height='35' src='", $captchaURI, "' />"; 
     echo "<input type='hidden' name='powerCode' value='", $_SESSION['captcha_key'], "'/>"; 
     if ($callType == "internal") 
     { 
      return ob_get_clean(); 
     } 
     ob_end_flush();