当前位置:首页 > PHP教程 > php图像处理 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2022-06-10 09:35:12 浏览: 评论:0 

验证码使用场景

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

代码:

  1. <?php 
  2.  
  3.  
  4.  
  5. /********************************************************************************* 
  6.  
  7.  * InitPHP 3.8.2 国产PHP开发框架   扩展类库-验证码 
  8.  
  9.  *------------------------------------------------------------------------------- 
  10.  
  11.  * 版权所有: CopyRight By initphp.com 
  12.  
  13.  * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己 
  14.  
  15.  *------------------------------------------------------------------------------- 
  16.  
  17.  * Author:zhuli Dtime:2014-11-25 
  18.  
  19.  ***********************************************************************************/ 
  20.  
  21. class Code 
  22.  
  23. { 
  24.  
  25.  
  26.  
  27.     private $charset = "abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";   //随机因子 
  28.  
  29.     private $code;     //验证码文字 
  30.  
  31.     private $codelen = 4;    //验证码显示几个文字 
  32.  
  33.     private $width = 100;   //验证码宽度 
  34.  
  35.     private $height = 40;   //验证码高度 
  36.  
  37.     private $img;       //验证码资源句柄 
  38.  
  39.     private $font;     //指定的字体 
  40.  
  41.     private $fontsize = 20;  //指定的字体大小 
  42.  
  43.     private $fontcolor;     //字体颜色  随机 
  44.  
  45.  
  46.  
  47.     //构造类  编写字体 
  48.  
  49.     public function __construct() 
  50.  
  51.     { 
  52.  
  53.         $this->font = '/outputs/font/font.ttf'; 
  54.  
  55.     } 
  56.  
  57.  
  58.  
  59.     //创建4个随机码 
  60.  
  61.     private function createCode() 
  62.  
  63.     { 
  64.  
  65.         $_leng = strlen($this->charset) - 1; 
  66.  
  67.         for ($i = 1; $i <= $this->codelen; $i++) { 
  68.  
  69.             $this->code .= $this->charset[mt_rand(0, $_leng)]; 
  70.  
  71.         } 
  72.  
  73. //        session_start(); 
  74.  
  75. //        $_SESSION['VerifyCode'] = strtolower($this->code); 
  76.  
  77.         Session::set('VerifyCode', strtolower($this->code)); 
  78.  
  79.         return $this->code; 
  80.  
  81.     } 
  82.  
  83.  
  84.  
  85.     //创建背景 
  86.  
  87.     private function createBg() 
  88.  
  89.     { 
  90.  
  91.         //创建画布 给一个资源jubing 
  92.  
  93.         $this->img = imagecreatetruecolor($this->width, $this->height); 
  94.  
  95.         //背景颜色 
  96.  
  97.         $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255)); 
  98.  
  99.         //画出一个矩形 
  100.  
  101.         imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color); 
  102.  
  103.     } 
  104.  
  105.  
  106.  
  107.     //创建字体 
  108.  
  109.     private function createFont() 
  110.  
  111.     { 
  112.  
  113.         $_x = ($this->width / $this->codelen);   //字体长度 
  114.  
  115.         for ($i = 0; $i < $this->codelen; $i++) { 
  116.  
  117.             //文字颜色 
  118.  
  119.             $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); 
  120.  
  121.             //资源句柄 字体大小 倾斜度 字体长度  字体高度  字体颜色  字体  具体文本 
  122.  
  123.             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]); 
  124.  
  125.         } 
  126.  
  127.     } 
  128.  
  129.  
  130.  
  131.     //随机线条 
  132.  
  133.     private function createLine() 
  134.  
  135.     { 
  136.  
  137.         //随机线条 
  138.  
  139.         for ($i = 0; $i < 6; $i++) { 
  140.  
  141.             $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); 
  142.  
  143.             imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color); 
  144.  
  145.         } 
  146.  
  147.         //随机雪花 
  148.  
  149.         for ($i = 0; $i < 45; $i++) { 
  150.  
  151.             $color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255)); 
  152.  
  153.             imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color); 
  154.  
  155.         } 
  156.  
  157.     } 
  158.  
  159.  
  160.  
  161.     //输出背景 
  162.  
  163.     private function outPut() 
  164.  
  165.     { 
  166.  
  167.         //生成标头 
  168.  
  169.         header('Content-type:image/png'); 
  170.  
  171.         //输出图片 
  172.  
  173.         imagepng($this->img); 
  174.  
  175.         //销毁结果集 
  176.  
  177.         imagedestroy($this->img); 
  178.  
  179.     } 
  180.  
  181.  
  182.  
  183.     //对外输出 
  184.  
  185.     public function doimg() 
  186.  
  187.     { 
  188.  
  189.         //加载背景 
  190.  
  191.         $this->createBg(); 
  192.  
  193.         //加载文件 
  194.  
  195.         $this->createCode(); 
  196.  
  197.         //加载线条 
  198.  
  199.         $this->createLine(); 
  200.  
  201.         //加载字体 
  202.  
  203.         $this->createFont(); 
  204.  
  205.         //加载背景 
  206.  
  207.         $this->outPut(); 
  208.  
  209.     } 
  210.  
  211.  
  212.  
  213.     //获取验证码 
  214.  
  215.     public function getCode() 
  216.  
  217.     { 
  218.  
  219.         return strtolower($this->code); 
  220.  
  221.     } 
  222.  
  223.  
  224.  
  225.     //验证验证码 
  226.  
  227.     public function checkCode($code, $clear = false) 
  228.  
  229.     { 
  230.  
  231. //        session_start(); 
  232.  
  233.         if (Session::get('VerifyCode') == strtolower($code)) { 
  234.  
  235.             if($clear) $this->clearCode(); 
  236.  
  237.             return true; 
  238.  
  239.         } 
  240.  
  241.         if($clear) $this->clearCode(); 
  242.  
  243.         return false; 
  244.  
  245.     } 
  246.  
  247.  
  248.  
  249.     //清除验证码 
  250.  
  251.     public function clearCode() 
  252.  
  253.     { 
  254.  
  255.         Session::del('VerifyCode'); 
  256.  
  257. //        session_start(); 
  258.  
  259. //        unset ($_SESSION['VerifyCode']); 
  260.  
  261.     } 
  262.  
  263. } 

验证:

ob_clean();

$verify = new Code();

$verify->doimg();

这样即可输出如下验证码

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

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

拓展

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

1. 加强干扰

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

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

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

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

2. 接入项目验证

新建个文件,代码如下

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

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

  1. //验证验证码 
  2.  
  3. public function checkCode($code, $clear = false) 
  4.  
  5. { 
  6.  
  7.     if (Session::get('VerifyCode') == strtolower($code)) { 
  8.  
  9.         if($clear) $this->clearCode(); 
  10.  
  11.             return true; 
  12.  
  13.     } 
  14.  
  15.     if($clear) $this->clearCode(); 
  16.  
  17.     return false; 
  18.  
  19. } 
  20.  
  21. //清除验证码 
  22.  
  23. public function clearCode() 
  24.  
  25. { 
  26.  
  27.     Session::del('VerifyCode'); 
  28.  
  29. } 

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

Tags: PHP生成图形验证码

分享到:

相关文章