2012-06-27 41 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();