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

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

调用页面显示验证码页面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. <style type="text/css">
  16. .table{
  17. font-family:Arial, Helvetica, sans-serif;
  18. font-size:12px;
  19. color:#333;
  20. background-color:#E4E4E4;
  21. }
  22. .table td{
  23. background-color:#F8F8F8;
  24. }
  25. </style>
  26. <form action="" method="post" name="form1" >
  27. <table width="400" align="center" cellpadding="5" cellspacing="1">
  28. <?php if(isset($msg)){?>
  29. <tr>
  30. <td colspan="2" align="center" valign="top"><?php echo $msg;?></td>
  31. </tr>
  32. <?php } ?>
  33. <tr>
  34. <td align="right" valign="top"> 验证码Demo:</td>
  35. <td><img src="captcha_code_file.php?rand=<?php echo rand(0,20);?>" onclick="refreshCaptcha();" ><br>
  36. <label for='message'>请输入验证码:</label>
  37. <br>
  38. <input name="6_letters_code" type="text">
  39. <br>
  40. 如果看不到,请 <a href='javascript: refreshCaptcha();'>点我</a> 刷新一下!
  41. </p></td>
  42. </tr>
  43. <tr>
  44. <td>&nbsp;</td>
  45. <td><input name="Submit" type="submit" onclick="return validate();" value="Submit"></td>
  46. </tr>
  47. </table>
  48. </form>
  49. <script type='text/javascript'>
  50. //定义的刷新请求
  51. function refreshCaptcha()
  52. {
  53. var img = document.images['captchaimg'];
  54. img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
  55. }
  56. </script>