百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>网页制作> PHP生成图形验证码(增强滋扰型)
分享文章到:

PHP生成图形验证码(增强滋扰型)

发布时间:09/01 来源:未知 浏览: 关键词:

验证码使用处景

我们在开发系统的历程中,根本所有的系统都会触及到登录模块,其中验证码功效是这里面必不成少的一块,是防止系统被爆破的有效途径。所谓道高一尺魔高一丈,此刻的验证码越来越复杂先进,常见的字母数字验证码,行动验证码。本文具体介绍简便的字母数字验证码。

代码

<?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. 接入项目验证

创建个文件,代码如下

<?php
ob_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生成图形验证码(增强干扰型)的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板