当前位置:首页 > PHP教程 > php图像处理 > 列表

完美的php生成验证码程序代码

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-18 15:24:37 浏览: 评论:0 

本文章给大家介绍一个非常不错的生成验证码的php程序,首先当用户请求HTTP的时候,服务器端就创建一个唯一的sessionid,这个是session会话ID,然后就去启动GD库或者imagemagick这些画图工具,把程序生成的随机的字符写到一张图片里面,然后显示到客户端,再由用户输入提交数据,然后我们程序把生成验证保存在session中进行比较,这样就完成了验证码的生成与验证了.

新建一个PHP文件captcha_code_file.php,代码如下:

  1. //首先开启session 
  2. session_start(); 
  3. //定义前台显示验证码长&宽 
  4. $image_width = 120; 
  5. $image_height = 40; 
  6. $characters_on_image = 6; 
  7. $font = './monofont.ttf'; 
  8.  
  9. //The characters that can be used in the CAPTCHA code. 
  10. //avoid confusing characters (l 1 and i for example) 
  11. $possible_letters = '23456789bcdfghjkmnpqrstvwxyz'; 
  12. $random_dots = 10; 
  13. $random_lines = 30; 
  14. $captcha_text_color="0x142864"; 
  15. $captcha_noice_color = "0x142864"; 
  16. //定义要生成验证码的字符串 
  17. $code = ''; 
  18.  
  19. $i = 0; 
  20. while ($i < $characters_on_image) {  
  21. $code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1); 
  22. $i++; 
  23. } 
  24.  
  25. $font_size = $image_height * 0.75; 
  26. $image = @imagecreate($image_width, $image_height); 
  27.  
  28. /* setting the background, text and noise colours here */ 
  29. $background_color = imagecolorallocate($image, 255, 255, 255); 
  30.  
  31. $arr_text_color = hexrgb($captcha_text_color); 
  32. $text_color = imagecolorallocate($image, $arr_text_color['red'],  
  33.         $arr_text_color['green'], $arr_text_color['blue']); 
  34.  
  35. $arr_noice_color = hexrgb($captcha_noice_color); 
  36. $image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],  
  37.         $arr_noice_color['green'], $arr_noice_color['blue']); 
  38.  
  39. /* generating the dots randomly in background */ 
  40. for( $i=0; $i<$random_dots; $i++ ) { 
  41. imagefilledellipse($image, mt_rand(0,$image_width), 
  42.  mt_rand(0,$image_height), 2, 3, $image_noise_color); 
  43. } 
  44.  
  45. /* generating lines randomly in background of image */ 
  46. for( $i=0; $i<$random_lines; $i++ ) { 
  47. imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height), 
  48.  mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color); 
  49. } 
  50.  
  51. /* create a text box and add 6 letters code in it */ 
  52. $textbox = imagettfbbox($font_size, 0, $font, $code);  
  53. $x = ($image_width - $textbox[4])/2; 
  54. $y = ($image_height - $textbox[5])/2; 
  55. imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code); 
  56.  
  57. /* Show captcha image in the page html page */ 
  58. header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow 
  59. imagejpeg($image);//showing the image 
  60. imagedestroy($image);//destroying the image instance 
  61. //设置session,做验证 
  62. //开源代码phpfensi.com 
  63. $_SESSION['6_letters_code'] = $code; 
  64.  
  65. function hexrgb ($hexstr) 
  66. { 
  67.   $int = hexdec($hexstr); 
  68.  
  69.   return array("red" => 0xFF & ($int >> 0x10), 
  70.                "green" => 0xFF & ($int >> 0x8), 
  71.                "blue" => 0xFF & $int); 
  72. } 

调用页面显示验证码页面index.php,代码如下:

  1. <?php 
  2. session_start(); 
  3. if(isset($_REQUEST['Submit'])){ 
  4.     // code for check server side validation 
  5.     if(emptyempty($_SESSION['6_letters_code'] ) || 
  6.         strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) 
  7.     {   
  8.         $msg="您输入的验证码有误,请重新输入!"; 
  9.     }else{ 
  10.         echo "您输入的是正确的!"; 
  11.         // Captcha verification is Correct. Final Code Execute here! 
  12.     } 
  13. }     
  14.  ?> 
  15.  
  16. <style type="text/css"> 
  17. .table{ 
  18.     font-family:Arial, Helvetica, sans-serif; 
  19.     font-size:12px; 
  20.     color:#333; 
  21.     background-color:#E4E4E4;     
  22. } 
  23. .table td{ 
  24.     background-color:#F8F8F8;     
  25. } 
  26. </style> 
  27.  
  28. <form action="" method="post" name="form1" id="form1" > 
  29.   <table width="400" border="0" align="center" cellpadding="5" cellspacing="1"> 
  30. <?php if(isset($msg)){?> 
  31.     <tr> 
  32.       <td colspan="2" align="center" valign="top"><?php echo $msg;?></td> 
  33.     </tr> 
  34. <?php } ?>  
  35.     <tr> 
  36.       <td align="right" valign="top"> 验证码Demo:</td> 
  37.       <td><img src="captcha_code_file.php?rand=<?php echo rand(0,20);?>" id='captchaimg'  onclick="refreshCaptcha();" ><br> 
  38.         <label for='message'>请输入验证码:</label> 
  39.         <br> 
  40.         <input id="6_letters_code" name="6_letters_code" type="text"> 
  41.         <br> 
  42.         如果看不到,请 <a href='javascript: refreshCaptcha();'>点我</a> 刷新一下! 
  43.         </p></td> 
  44.     </tr> 
  45.     <tr> 
  46.       <td>&nbsp;</td> 
  47.       <td><input name="Submit" type="submit" onclick="return validate();" value="Submit"></td> 
  48.     </tr> 
  49.   </table> 
  50. </form> 
  51. <script type='text/javascript'> 
  52. //定义的刷新请求 
  53. function refreshCaptcha() 
  54. { 
  55.     var img = document.images['captchaimg']; 
  56.     img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000; 
  57. } 
  58. </script> 

Tags: php生成 验证码 图片

分享到: