前言
验证码是一种区分用户是计算机和人的公共全自动程序。简单来说,验证码就是验证操作是人还是机器。下面我就总结一下常见的验证码类型都有哪些?

数字、字母组合
这种形式最为常见,也很简单。有的是单独使用这两种,也有的是数字、字母混合而成,为了提高识别难度,有的会添加干扰线,如在背景中添加干扰线。

1<?php 2// 丢弃输出缓冲区的内容 ** 3ob_clean(); 4 5// 创建画布 6$image = imagecreatetruecolor(110, 30); 7 8// 设置白色底 9$bgColor = imagecolorallocate($image, 255, 255, 255); 10imagefill($image, 0, 0, $bgColor); 11 12// 添加四个随机数字字母 13for($i=0;$i<4;$i++) { 14 $fontSize = 6; 15 // 随机分配颜色 16 $fontColor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120)); 17 // 生成内容 18 $data = "abcdefghijkmnpqrstuvwxy3456789"; 19 // 如果内容为空,重新输出1个 20 do { 21 $fontCont = substr($data, rand(0, strlen($data)), 1); 22 } while ($fontCont == ''); 23 // 设置范围 24 $x = ($i*110/4)+rand(5, 10); 25 $y = rand(5, 10); 26 // 图片加入数字 27 imagestring($image, $fontSize, $x, $y, $fontCont, $fontColor); 28} 29 30// 添加干扰点元素 31for($j=0;$j<200;$j++) { 32 // 点颜色 33 $pointColor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200)); 34 imagesetpixel($image, rand(1, 99), rand(1, 29), $pointColor); 35} 36 37// 添加干扰线元素 38for($z=0;$z<4;$z++) { 39 // 生成颜色线 40 $lineColor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220)); 41 imageline($image, rand(1, 99), rand(1, 29), rand(1, 99), rand(1, 29), $lineColor); 42} 43 44header("Content-type:image/png"); 45// 输出图片 46imagepng($image); 47// 销毁内存中的图片 48imagedestroy($image); 49 50?>
短信验证码
随着手机的普及,很多APP都是用手机号注册的。为了验证手机号码的真实性,防止恶意注册,通常会向手机发送验证码。网上有专门的短信发送平台,向电信运营商支付短信费用,接入即可使用。

图片识别
根据提示,点击对应的元素。逻辑解题能力结合图形符号等元素识别能力。适用于安全要求超高的业务场景。
使用KgCaptcha,在用户控制台设置验证类型,多种类型选择,如滑动拼图、文字点选、语序点选、字体识别、空间推理。

1<script src="captcha.js?appid=xxx"></script> 2<script> 3kg.captcha({ 4 // 绑定元素,验证框显示区域 5 bind: "#captchaBox2", 6 // 验证成功事务处理 7 success: function(e) { 8 console.log(e); 9 }, 10 // 验证失败事务处理 11 failure: function(e) { 12 console.log(e); 13 }, 14 // 点击刷新按钮时触发 15 refresh: function(e) { 16 console.log(e); 17 } 18}); 19</script> 20<div id="captchaBox2">载入中 ...</div>
最后
SDK开源地址:KgCaptcha (KgCaptcha) · GitHub,顺便做了一个演示:凯格行为验证码在线体验