PHP to generate five kinds of image codes Examples

PHP to generate five kinds of authentication code examples, including digital codes, digital codes + letters, Chinese codes, and arithmetic codes imitation google verification code, the verification code generation principle PHP: GD library by generating codes with a pictures, and verification code in Session.

js validation

 1 $(function() { 
 2    $("#getcode_num").click(function() {  //数字验证 
 3         $(this).attr("src", 'code_num.php?' + Math.random()); 
 4     }); 
 5     $("#chk_num").click(function() { 
 6         var code_num = $("#code_num").val(); 
 7         $.post("chk_code.php?act=num", { 
 8             code: code_num 
 9         }, 
10         function(msg) { 
11             if (msg == 1) { 
 12 is                  Alert ( "code is correct!" ); 
 13 is              } the else { 
 14                  Alert ( "Error codes!" ); 
 15              } 
 16          }); 
 17      }); 
 18      // digital verification letter + 
19      $ ( "# getcode_char. ") the Click ( function () { 
 20 is          $ ( the this ) .attr (" the src ", 'code_char.php?' + Math.random ()); 
 21 is      }); 
 22 is      $ (" # chk_char ") the Click. ( function () { 
 23 is          var code_char = $ ( "#code_char") .val (); 
 24          $ .post ( "ACT chk_code.php = char?" , { 
 25              code: code_char 
 26 is          }, 
 27          function (MSG) { 
 28              IF (MSG ==. 1 ) { 
 29                  Alert ( "codes right! " ); 
 30              } the else { 
 31 is                  Alert (" error codes "! ); 
 32              } 
 33          }); 
 34      }); 
 35      // Chinese codes 
36      $ (." # getcode_zh ") the Click ( function () { 
 37         $(this).attr("src", 'code_zh.php?' + Math.random()); 
38     }); 
39     $("#chk_zh").click(function() { 
40         var code_zh = escape($("#code_zh").val()); 
41         $.post("chk_code.php?act=zh", { 
42             code: code_zh 
43         }, 
44         function(msg) { 
45             if (msg == 1) { 
46                 alert("验证码正确!"); 
47             } else { 
48                 alert("验证码错误!"); 
49             } 
50         }); 
51     }); 
52     //google验证 
53     $("#getcode_gg").click(function() { 
54         $(this).attr("src", 'code_gg.php?' + Math.random()); 
55     }); 
56     $("#chk_gg").click(function() { 
57         var code_gg = $("#code_gg").val(); 
58         $.post("chk_code.php?act=gg", { 
59             code: code_gg 
60         }, 
61 is          function (MSG) { 
 62 is              IF (MSG ==. 1 ) { 
 63 is                  Alert ( "code is correct!" ); 
 64              } the else { 
 65                  Alert ( "Error codes!" ); 
 66              } 
 67          }); 
 68      }) ; 
 69      // arithmetic verification 
70      $ ( "# getcode_math") the Click. ( function () { 
 71 is          $ ( the this ) .attr ( "the src", 'code_math.php?' + Math.random ()); 
 72      }) ; 
 73      $ ( "# chk_math") the Click (.function () { 
 74          var code_math = $ ( "# code_math" ) .val (); 
 75          $ .post ( "ACT = chk_code.php Math?" , { 
 76              code: code_math 
 77          }, 
 78          function (MSG) { 
 79              IF (MSG ==. 1 ) { 
 80                  Alert ( "code is correct!" ); 
 81              } the else { 
 82                  Alert ( "error codes!" ); 
 83              } 
 84          }); 
 85      }); 
 86 });


Generating codes

. 1  session_start ();  
 2 the getCode (4,60,20 );  
 . 3    
. 4  function the getCode ( $ NUM , $ W , $ H ) {  
 . 5      $ code = "" ;  
 . 6      for ( $ I = 0; $ I < $ NUM ; $ I ++ ) {  
 . 7          $ code =. RAND (0,. 9 );  
 . 8      }  
 . 9      // . 4 bit authentication code may be generated directly RAND (1000,9999)  
 10      // write the generated codes session when backup verification   
. 11      $ _SESSION[ "helloweba_num"] = $ code ;  
 12 is      // create an image, the definition of color values   
13 is      header ( "the Content-type: Image / PNG" );  
 14      $ IM = imagecreate ( $ W , $ H );  
 15      $ Black = imagecolorallocate ( $ IM , 0, 0, 0 );  
 16      $ Gray = imagecolorallocate ( $ IM , 200 is, 200 is, 200 is );  
 . 17      $ bgcolor = imagecolorallocate ( $ IM , 255, 255, 255 );  
 18 is      // fill the background   
. 19      ImageFill ( $ IM , 0, 0,Gray $ );  
 20 is    
21 is      // Videos frame   
22 is      imagerectangle ( $ IM , 0, 0, $ W -1, $ H -1, $ Black );  
 23 is    
24      // random draw two dotted line, from the interference   
25      $ style = Array ( $ Black , $ Black , $ Black , $ Black , $ Black ,  
 26 is          $ Gray , $ Gray , $ Gray , $ Gray , $ Gray   
27      );  
 28      imagesetstyle ($im, $style);  
29     $y1 = rand(0, $h);  
30     $y2 = rand(0, $h);  
31     $y3 = rand(0, $h);  
32     $y4 = rand(0, $h);  
33     imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED);  
34     imageline($im, 0, $y2, $w, $ Y4 , IMG_COLOR_STYLED);  
 35    
36      // randomly generated a large number of black spots on the canvas, since interference;   
37 [      for ( $ I = 0; $ I <80; $ I ++ ) {  
 38 is          imagesetpixel ( $ IM , RAND (0, $ W ), RAND (0, $ H ), $ Black );  
 39      }  
 40      // the random number displayed on the canvas, and the horizontal position of the character spacing are generated by a certain random fluctuation range   
41 is      $ STRx = RAND (. 3,. 8 );  
 42 is      for ( $ I = 0; $i < $num; $i++) {  
43         $strpos = rand(1, 6);  
44         imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black);  
45         $strx += rand(8, 12);  
46     }  
47     imagepng($im);//输出图片  
48     imagedestroy($im);// release the memory occupied Picture   
49 }


This switched: https://www.sucaihuo.com/php/91.html please indicate the source!

Guess you like

Origin www.cnblogs.com/zglevk/p/12359324.html