PHP generates graphic verification code (enhanced interference type)

安安杰尼
Release: 2023-04-08 15:16:01
Original
195678 people have browsed it

Verification code usage scenario

In the process of developing the system, basically all systems will involve the login module, of which the verification code function is an indispensable part to prevent the system from being exploded. Effective Ways. As the saying goes, the devil is as high as the road. Nowadays, verification codes are becoming more and more complex and advanced. Common alphanumeric verification codes and behavioral verification codes are common. This article details simple alphanumeric verification codes.

Code

<?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 = &#39;/outputs/font/font.ttf&#39;;
    }

    //创建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[&#39;VerifyCode&#39;] = strtolower($this->code);
        Session::set(&#39;VerifyCode&#39;, 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), &#39;*&#39;, $color);
        }
    }

    //输出背景
    private function outPut()
    {
        //生成标头
        header(&#39;Content-type:image/png&#39;);
        //输出图片
        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(&#39;VerifyCode&#39;) == strtolower($code)) {
            if($clear) $this->clearCode();
            return true;
        }
        if($clear) $this->clearCode();
        return false;
    }

    //清除验证码
    public function clearCode()
    {
        Session::del(&#39;VerifyCode&#39;);
//        session_start();
//        unset ($_SESSION[&#39;VerifyCode&#39;]);
    }
}
Copy after login

Verification

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

This will output the following verification code

PHP generates graphic verification code (enhanced interference type)

Parameters can be adjusted to control the size of the verification code, interference items, etc.

Expansion

Next, we will introduce the expansion function, how to strengthen the interference items of the verification code, and how to integrate it into the project for login verification.

1. Strengthen interference

First of all, we can see a few lines in the screenshot above. If outsiders use analysis tools to decode, it will be very simple to solve the problem. To generate our verification code, you need to add the number of lines at this time. Find the following code in the code and modify the number 6 above

//随机线条
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), &#39;*&#39;, $color);
    }
}
Copy after login

. You can adjust it slowly, and then check the effect until you are satisfied. At the same time, you can see that there are many snowflake effects in the verification code. This is also an interference item. You can modify the number 45 above to adjust to a satisfactory result.

Note: The $charset variable in the code, the verification code randomly picks characters from here to survive verification, because the effect of lowercase i and L is difficult to distinguish, So we removed the i character.

2. Access project verification

Create a new file with the following code

Copy after login

Then introduce this into the existing system login page The interface can display the verification code. After the user fills in and submits, the server performs the following verification

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

At this point, the verification code generation and verification process have been completed.

The above is the detailed content of PHP generates graphic verification code (enhanced interference type). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template