A-A+

PHP生成图形验证码(加强干扰型)

2022年06月10日 我爱编程 暂无评论

验证码使用场景

我们在开发系统的过程中,基本所有的系统都会涉及到登录模块,其中验证码功能是这里面必不可少的一块,是防止系统被爆破的有效途径。所谓道高一尺魔高一丈,现在的验证码越来越复杂先进,常见的字母数字验证码,行为验证码。本文详细介绍简单的字母数字验证码。

代码:

  1. <?php
  2. /*********************************************************************************
  3.  * InitPHP 3.8.2 国产PHP开发框架   扩展类库-验证码
  4.  *-------------------------------------------------------------------------------
  5.  * 版权所有: CopyRight By initphp.com
  6.  * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  7.  *-------------------------------------------------------------------------------
  8.  * Author:zhuli Dtime:2014-11-25
  9.  ***********************************************************************************/
  10. class Code
  11. {
  12.     private $charset = "abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";   //随机因子
  13.     private $code;     //验证码文字
  14.     private $codelen = 4;    //验证码显示几个文字
  15.     private $width = 100;   //验证码宽度
  16.     private $height = 40;   //验证码高度
  17.     private $img;       //验证码资源句柄
  18.     private $font;     //指定的字体
  19.     private $fontsize = 20;  //指定的字体大小
  20.     private $fontcolor;     //字体颜色  随机
  21.     //构造类  编写字体
  22.     public function __construct()
  23.     {
  24.         $this->font = '/outputs/font/font.ttf';
  25.     }
  26.     //创建4个随机码
  27.     private function createCode()
  28.     {
  29.         $_leng = strlen($this->charset) - 1;
  30.         for ($i = 1; $i <= $this->codelen; $i++) {
  31.             $this->code .= $this->charset[mt_rand(0, $_leng)];
  32.         }
  33. //        session_start();
  34. //        $_SESSION['VerifyCode'] = strtolower($this->code);
  35.         Session::set('VerifyCode'strtolower($this->code));
  36.         return $this->code;
  37.     }
  38.     //创建背景
  39.     private function createBg()
  40.     {
  41.         //创建画布 给一个资源jubing
  42.         $this->img = imagecreatetruecolor($this->width, $this->height);
  43.         //背景颜色
  44.         $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
  45.         //画出一个矩形
  46.         imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
  47.     }
  48.     //创建字体
  49.     private function createFont()
  50.     {
  51.         $_x = ($this->width / $this->codelen);   //字体长度
  52.         for ($i = 0; $i < $this->codelen; $i++) {
  53.             //文字颜色
  54.             $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
  55.             //资源句柄 字体大小 倾斜度 字体长度  字体高度  字体颜色  字体  具体文本
  56.             imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $color$this->font, $this->code[$i]);
  57.         }
  58.     }
  59.     //随机线条
  60.     private function createLine()
  61.     {
  62.         //随机线条
  63.         for ($i = 0; $i < 6; $i++) {
  64.             $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
  65.             imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
  66.         }
  67.         //随机雪花
  68.         for ($i = 0; $i < 45; $i++) {
  69.             $color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));
  70.             imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*'$color);
  71.         }
  72.     }
  73.     //输出背景
  74.     private function outPut()
  75.     {
  76.         //生成标头
  77.         header('Content-type:image/png');
  78.         //输出图片
  79.         imagepng($this->img);
  80.         //销毁结果集
  81.         imagedestroy($this->img);
  82.     }
  83.     //对外输出
  84.     public function doimg()
  85.     {
  86.         //加载背景
  87.         $this->createBg();
  88.         //加载文件
  89.         $this->createCode();
  90.         //加载线条
  91.         $this->createLine();
  92.         //加载字体
  93.         $this->createFont();
  94.         //加载背景
  95.         $this->outPut();
  96.     }
  97.     //获取验证码
  98.     public function getCode()
  99.     {
  100.         return strtolower($this->code);
  101.     }
  102.     //验证验证码
  103.     public function checkCode($code$clear = false)
  104.     {
  105. //        session_start();
  106.         if (Session::get('VerifyCode') == strtolower($code)) {
  107.             if($clear$this->clearCode();
  108.             return true;
  109.         }
  110.         if($clear$this->clearCode();
  111.         return false;
  112.     }
  113.     //清除验证码
  114.     public function clearCode()
  115.     {
  116.         Session::del('VerifyCode');
  117. //        session_start();
  118. //        unset ($_SESSION['VerifyCode']);
  119.     }
  120. }

验证:

ob_clean();

$verify = new Code();

$verify->doimg();

这样即可输出如下验证码

PHP生成图形验证码(加强干扰型)

可以调整参数控制验证码的大小,干扰项等。

拓展

接下来介绍下拓展的功能,怎么加强验证码的干扰项,怎么结合到项目丽进行登录验证。

1. 加强干扰

首先我们可以看到上面的截图中少数线条,如果外者使用分析工具来解码,那么会很简单的就解出我们的验证码,这时候就需要添加线条的数量,在代码中找到以下代码并修改

  1. //随机线条
  2. private function createLine()
  3. {
  4.     //随机线条 
  5.     for ($i = 0; $i < 6; $i++) {
  6.         $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
  7.         imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
  8.     }
  9.     //随机雪花
  10.     for ($i = 0; $i < 45; $i++) {
  11.         $color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));
  12.         imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*'$color);
  13.     }
  14. }

上面的数字6可以慢慢调整,然后查看效果直到满意。同时可以看到验证码中有很多雪花效果,这个也是干扰项,可以修改上面的数字45来调整到自己满意的结果。

注意:代码中的$charset变量,验证码是从这边随机取字符来生存验证,由于小写的i和L展示的效果很难分辨,所以我们去除了i字符。

2. 接入项目验证

新建个文件,代码如下

  1. <?php
  2. ob_clean();
  3. $verify = new Code();
  4. $verify->doimg();

然后在现有的系统登录页面引入这个接口即可展示验证码,在用户填写提交之后,服务端做以下验证

  1. //验证验证码
  2. public function checkCode($code$clear = false)
  3. {
  4.     if (Session::get('VerifyCode') == strtolower($code)) {
  5.         if($clear$this->clearCode();
  6.             return true;
  7.     }
  8.     if($clear$this->clearCode();
  9.     return false;
  10. }
  11. //清除验证码
  12. public function clearCode()
  13. {
  14.     Session::del('VerifyCode');
  15. }

至此,验证码的生成以及验证流程都已完成。

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录