当前位置:Gxlcms > php框架 > php生成图片验证码-附五种验证码

php生成图片验证码-附五种验证码

时间:2021-07-01 10:21:17 帮助过:45人阅读

以前输出验证码的时候用过一个方法,在前台用JS生成验证码字符串,再传递到后台用PHP输出验证码图像。这样在验证时就不需要使用$_SESSION传递验证码的值,直接用JS比较生成的字符串和输入的字符串是否相等即可。

本文以实例演示5种验证码,并介绍生成验证码的函数。PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session中。

1、HTML

5中验证码HTML代码如下:

  1. <div class="demo">
  2. <h3>1、数字验证码</h3>
  3. <p>验证码:<input type="text" class="input" id="code_num" name="code_num" maxlength="4" /> <img src="code_num.php" id="getcode_num" title="看不清,点击换一张" align="absmiddle" /></p>
  4. <p><input type="button" class="btn" id="chk_num" value="提交" /></p>
  5. </div>
  6. <div class="demo">
  1. <h3>2、数字+字母验证码</h3>
  2. <p>验证码:<input type="text" class="input" id="code_char" maxlength="4" /> <img src="code_char.php" id="getcode_char" title="看不清,点击换一张" align="absmiddle" /></p>
  3. <p><input type="button" class="btn" id="chk_char" value="提交" /></p>
  4. </div>
  5. <div class="demo">

  1. <h3>3、中文验证码</h3>
  2. <p>验证码:<input type="text" class="input" id="code_zh" maxlength="4" /> <img src="code_zh.php" id="getcode_zh" title="看不清,点击换一张" align="absmiddle" /></p>
  3. <p><input type="button" class="btn" id="chk_zh" value="提交" /></p>
  4. </div>
  5. <div class="demo">

  1. <h3>4、仿google验证码</h3>
  2. <p>验证码:<input type="text" class="input" id="code_gg" maxlength="4" /> <img src="code_gg.php" id="getcode_gg" title="看不清,点击换一张" align="absmiddle" /></p>
  3. <p><input type="button" class="btn" id="chk_gg" value="提交" /></p>
  4. </div>
  5. <div class="demo">

 
  1. <h3>5、算术验证码</h3>
  2. <p>验证码:<input type="text" class="input" id="code_math" maxlength="4" /> <img src="code_math.php" id="getcode_math" title="看不清,点击换一张" align="absmiddle" /></p>
  3. <p><input type="button" class="btn" id="chk_math" value="提交" /></p>
  4. </div>

2、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. });

    //数字+字母验证

  1. $("#getcode_char").click(function() {
  2. $(this).attr("src", 'code_char.php?' + Math.random());
  3. });
  4. $("#chk_char").click(function() {
  5. var code_char = $("#code_char").val();
  6. $.post("chk_code.php?act=char", {
  7. code: code_char
  8. },
  9. function(msg) {
  10. if (msg == 1) {
  11. alert("验证码正确!");
  12. } else {
  13. alert("验证码错误!");
  14. }
  15. });
  16. });

    //中文验证码

  1. $("#getcode_zh").click(function() {
  2. $(this).attr("src", 'code_zh.php?' + Math.random());
  3. });
  4. $("#chk_zh").click(function() {
  5. var code_zh = escape($("#code_zh").val());
  6. $.post("chk_code.php?act=zh", {
  7. code: code_zh
  8. },
  9. function(msg) {
  10. if (msg == 1) {
  11. alert("验证码正确!");
  12. } else {
  13. alert("验证码错误!");
  14. }
  15. });
  16. });

    //google验证

  1. $("#getcode_gg").click(function() {
  2. $(this).attr("src", 'code_gg.php?' + Math.random());
  3. });
  4. $("#chk_gg").click(function() {
  5. var code_gg = $("#code_gg").val();
  6. $.post("chk_code.php?act=gg", {
  7. code: code_gg
  8. },
  9. function(msg) {
  10. if (msg == 1) {
  11. alert("验证码正确!");
  12. } else {
  13. alert("验证码错误!");
  14. }
  15. });
  16. });

    //算术验证 

  1. $("#getcode_math").click(function() {
  2. $(this).attr("src", 'code_math.php?' + Math.random());
  3. });
  4. $("#chk_math").click(function() {
  5. var code_math = $("#code_math").val();
  6. $.post("chk_code.php?act=math", {
  7. code: code_math
  8. },
  9. function(msg) {
  10. if (msg == 1) {
  11. alert("验证码正确!");
  12. } else {
  13. alert("验证码错误!");
  14. }
  15. });
  16. });
  17. });

3、PHP生成验证码

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

以上内容就是php生成图片验证码-附五种验证码的全部内容,希望大家喜欢。

人气教程排行