10种最全的PHP生成各种前端验证码和Ajax验证

jerry PHP 2015年08月10日 收藏

PHP生成验证码图片
PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中。PHP生成验证码的大致流程有:
1、产生一张png的图片;
2、为图片设置背景色;
3、设置字体颜色和样式;
4、产生4位数的随机的验证码;
5、把产生的每个字符调整旋转角度和位置画到png图片上;
6、加入噪点和干扰线防止注册机器分析原图片来恶意po,jie验证码;
7、输出图片;
8、释放图片所占内存

代码如下:

  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. }

代码中,自定义函数getCode()诠释了验证码的生成过程。运用PHP GD库自带的图像处理函数,能轻松生成各种想要的图片效果。
imagecreate():创建一个新图像
imagecolorallocate():为图像分配颜色
imagefill():填充图像
imagerectangle():画一个矩形(边框)
imagesetstyle():设置画线风格
imageline():画一条线段
imagesetpixel():画点像素
imagepng():以PNG格式将图像输出到浏览器或文件
imagedestroy():释放图片所占内存
将上述代码保存为code_num.php,以便调用。
Ajax刷新和验证
验证码生成后,我们要在实际的项目中应用,通常我们使用ajax可以实现点击验证码时刷新生成新的验证码(有时生成的验证码肉眼很难识别),即“看不清换一张”。填写验证码后,还需要验证所填验证码是否正确,验证的过程是要后台程序来完成,但是我们也可以通过ajax来实现无刷新验证。
建立一个页面index.html,引入jquery,同时在body中加入验证码表单元素:

  1. <p>验证码:<input type="text" class="input" id="code_num" name="code_num" maxlength="4" />  
  2. <img src="code_num.php" id="getcode_num" title="看不清,点击换一张" align="absmiddle"></p> 
  3. <p><input type="button" class="btn" id="chk_num" value="提交" /></p>

html代码中,<img src="code_num.php"即调用了生成的验证码,当点击验证码时,刷新生成新的验证码:

  1. $(function(){ 
  2.     //数字验证 
  3.     $("#getcode_num").click(function(){ 
  4.         $(this).attr("src",'code_num.php?' + Math.random()); 
  5.     }); 
  6.     ... 
  7. });

刷新验证码,其实就是重新请求了验证码生成程序,这里要注意的是调用code_num.php时要带上随机参数防止缓存。接下来填写好验证码之后,点“提交”按钮,通过$.post(),前端向后台chk_code.php发送ajax请求。

  1. $(function(){ 
  2.     ... 
  3.     $("#chk_num").click(function(){ 
  4.         var code_num = $("#code_num").val(); 
  5.         $.post("chk_code.php?act=num",{code:code_num},function(msg){ 
  6.             if(msg==1){ 
  7.                 alert("验证码正确!"); 
  8.             }else{ 
  9.                 alert("验证码错误!"); 
  10.             } 
  11.         }); 
  12.     }); 
  13. });

后台chk_code.php验证:

  1. session_start(); 
  2.  
  3. $code = trim($_POST['code']); 
  4. if($code==$_SESSION["helloweba_num"]){ 
  5.    echo '1'; 
  6. }

后台根据提交的验证码与保存在session中的验证码比对,完成验证。

下载地址