侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

php  /  管理员 发布于 7年前   187

验证码使用场景

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

代码

<?php/********************************************************************************* * InitPHP 3.8.2 国产PHP开发框架   扩展类库-验证码 *------------------------------------------------------------------------------- * 版权所有: CopyRight By initphp.com * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己 *------------------------------------------------------------------------------- * Author:zhuli Dtime:2014-11-25 ***********************************************************************************/class Code{    private $charset = "abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";   //随机因子    private $code;     //验证码文字    private $codelen = 4;    //验证码显示几个文字    private $width = 100;   //验证码宽度    private $height = 40;   //验证码高度    private $img;       //验证码资源句柄    private $font;     //指定的字体    private $fontsize = 20;  //指定的字体大小    private $fontcolor;     //字体颜色  随机    //构造类  编写字体    public function __construct()    {        $this->font = '/outputs/font/font.ttf';    }    //创建4个随机码    private function createCode()    {        $_leng = strlen($this->charset) - 1;        for ($i = 1; $i <= $this->codelen; $i++) {$this->code .= $this->charset[mt_rand(0, $_leng)];        }//        session_start();//        $_SESSION['VerifyCode'] = strtolower($this->code);        Session::set('VerifyCode', strtolower($this->code));        return $this->code;    }    //创建背景    private function createBg()    {        //创建画布 给一个资源jubing        $this->img = imagecreatetruecolor($this->width, $this->height);        //背景颜色        $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));        //画出一个矩形        imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);    }    //创建字体    private function createFont()    {        $_x = ($this->width / $this->codelen);   //字体长度        for ($i = 0; $i < $this->codelen; $i++) {//文字颜色$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));//资源句柄 字体大小 倾斜度 字体长度  字体高度  字体颜色  字体  具体文本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]);        }    }    //随机线条    private function createLine()    {        //随机线条        for ($i = 0; $i < 6; $i++) {$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);        }        //随机雪花        for ($i = 0; $i < 45; $i++) {$color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);        }    }    //输出背景    private function outPut()    {        //生成标头        header('Content-type:image/png');        //输出图片        imagepng($this->img);        //销毁结果集        imagedestroy($this->img);    }    //对外输出    public function doimg()    {        //加载背景        $this->createBg();        //加载文件        $this->createCode();        //加载线条        $this->createLine();        //加载字体        $this->createFont();        //加载背景        $this->outPut();    }    //获取验证码    public function getCode()    {        return strtolower($this->code);    }    //验证验证码    public function checkCode($code, $clear = false)    {//        session_start();        if (Session::get('VerifyCode') == strtolower($code)) {if($clear) $this->clearCode();return true;        }        if($clear) $this->clearCode();        return false;    }    //清除验证码    public function clearCode()    {        Session::del('VerifyCode');//        session_start();//        unset ($_SESSION['VerifyCode']);    }}

验证

ob_clean();$verify = new Code();$verify->doimg();

这样即可输出如下验证码

WX20200429-205016.png

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

拓展

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

1. 加强干扰

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

//随机线条private function createLine(){    //随机线条     for ($i = 0; $i < 6; $i++) {        $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));        imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);    }    //随机雪花    for ($i = 0; $i < 45; $i++) {        $color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));        imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);    }}

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

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

2. 接入项目验证

新建个文件,代码如下

<?phpob_clean();$verify = new Code();$verify->doimg();

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

//验证验证码public function checkCode($code, $clear = false){    if (Session::get('VerifyCode') == strtolower($code)) {        if($clear) $this->clearCode();return true;    }    if($clear) $this->clearCode();    return false;}//清除验证码public function clearCode(){    Session::del('VerifyCode');}

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

以上就是PHP生成图形验证码(加强干扰型)的详细内容,更多请关注其它相关文章!


  • 上一条:
    PHP常用日期时间操作合集
    下一条:
    php获取Http请求的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客