zoukankan      html  css  js  c++  java
  • 5种PHP生成图片验证码实例

    5种PHP生成图片验证码实例,包括数字验证码、数字+字母验证码、中文验证码、仿google验证码和算术验证码,PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session中。

    js验证

     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                 alert("验证码正确!"); 
    13             } else { 
    14                 alert("验证码错误!"); 
    15             } 
    16         }); 
    17     }); 
    18     //数字+字母验证 
    19     $("#getcode_char").click(function() { 
    20         $(this).attr("src", 'code_char.php?' + Math.random()); 
    21     }); 
    22     $("#chk_char").click(function() { 
    23         var code_char = $("#code_char").val(); 
    24         $.post("chk_code.php?act=char", { 
    25             code: code_char 
    26         }, 
    27         function(msg) { 
    28             if (msg == 1) { 
    29                 alert("验证码正确!"); 
    30             } else { 
    31                 alert("验证码错误!"); 
    32             } 
    33         }); 
    34     }); 
    35     //中文验证码 
    36     $("#getcode_zh").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         function(msg) { 
    62             if (msg == 1) { 
    63                 alert("验证码正确!"); 
    64             } else { 
    65                 alert("验证码错误!"); 
    66             } 
    67         }); 
    68     }); 
    69     //算术验证 
    70     $("#getcode_math").click(function() { 
    71         $(this).attr("src", 'code_math.php?' + Math.random()); 
    72     }); 
    73     $("#chk_math").click(function() { 
    74         var code_math = $("#code_math").val(); 
    75         $.post("chk_code.php?act=math", { 
    76             code: code_math 
    77         }, 
    78         function(msg) { 
    79             if (msg == 1) { 
    80                 alert("验证码正确!"); 
    81             } else { 
    82                 alert("验证码错误!"); 
    83             } 
    84         }); 
    85     }); 
    86 });


    生成验证码

     1 session_start();  
     2 getCode(4,60,20);  
     3   
     4 function getCode($num,$w,$h) {  
     5     $code = "";  
     6     for ($i = 0; $i < $num; $i++) {  
     7         $code .= rand(0, 9);  
     8     }  
     9     //4位验证码也可以用rand(1000,9999)直接生成  
    10     //将生成的验证码写入session,备验证时用  
    11     $_SESSION["helloweba_num"] = $code;  
    12     //创建图片,定义颜色值  
    13     header("Content-type: image/PNG");  
    14     $im = imagecreate($w, $h);  
    15     $black = imagecolorallocate($im, 0, 0, 0);  
    16     $gray = imagecolorallocate($im, 200, 200, 200);  
    17     $bgcolor = imagecolorallocate($im, 255, 255, 255);  
    18     //填充背景  
    19     imagefill($im, 0, 0, $gray);  
    20   
    21     //画边框  
    22     imagerectangle($im, 0, 0, $w-1, $h-1, $black);  
    23   
    24     //随机绘制两条虚线,起干扰作用  
    25     $style = array ($black,$black,$black,$black,$black,  
    26         $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     //在画布上随机生成大量黑点,起干扰作用;  
    37     for ($i = 0; $i < 80; $i++) {  
    38         imagesetpixel($im, rand(0, $w), rand(0, $h), $black);  
    39     }  
    40     //将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成  
    41     $strx = rand(3, 8);  
    42     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);//释放图片所占内存  
    49 }


    本文转自:https://www.sucaihuo.com/php/91.html 转载请注明出处!

  • 相关阅读:
    构建工具
    Ajax跨域问题
    Flex 布局教程:语法篇
    Linux常用命令
    JavaScript正则表达式
    jQuery基础(四)动画
    前端面试问题汇总(一)
    jQuery基础(三)事件
    JavaScript中的基本数据类型
    Django积木块二——邮箱
  • 原文地址:https://www.cnblogs.com/zglevk/p/12359324.html
Copyright © 2011-2022 走看看