Table of Contents
Verification code parameters
Verification code font
Background image
Chinese verification code
Verification code detection
Home PHP Framework ThinkPHP Detailed explanation of verification code of ThinkPHP framework

Detailed explanation of verification code of ThinkPHP framework

Oct 28, 2020 pm 03:35 PM
thinkphp

The following is the tutorial column of ThinkPHP to introduce you to the verification code of the ThinkPHP framework. I hope it will be helpful to friends in need!

Think\Verify class can support the generation and verification functions of verification code.

The following is the simplest way to generate a verification code:

$Verify = new \Think\Verify();$Verify->entry();
Copy after login

The above code will generate a default verification code image and output it, as shown below:

The generated verification code information will be saved in the session, and the data included are:

array('verify_code'=>'当前验证码的值','verify_time'=>'验证码生成的时间戳')
Copy after login

If you need to generate multiple verification codes on one page, the entry method needs to pass in the Identified information.

Verification code parameters

You can set relevant parameters for the generated verification code to achieve different display effects. These parameters include:

Parameter Description
expire Validity period of verification code (seconds)
useImgBg Whether to use a background image, the default is false
fontSize Verification code font size (pixels) The default is 25
useCurve Whether to use the confusion curve the default is true
useNoise Whether to add noise points by default is true
imageW The verification code width is set to 0 for automatic calculation
imageH Set the verification code height to 0 for automatic calculation
length Verification code digits
fontttf The specified verification code font defaults to random acquisition
useZh Whether to use Chinese verification code
bg Verification code background color rgb array setting, such as array(243, 251, 254)
seKey Encryption key of verification code
codeSet Verification code character set 3.2.1 Newly added
zhSet Verification code character set (Chinese) 3.2.1 Added

Default parameter configuration:

Parameter setting uses two methods.

Instantiate the incoming parameters:

or use dynamic settings, such as:

Verification code font

By default, the font of the verification code is randomly used in the font file under the ThinkPHP/Library/Think/Verify/ttfs/ directory. We can specify the font of the verification code. , for example:

$Verify = new \Think\Verify();// 验证码字体使用 ThinkPHP/Library/Think/Verify/ttfs/5.ttf$Verify->fontttf = '5.ttf';$Verify->entry();
Copy after login

Background image

supports the verification code background image function, which can be set as follows:

$Verify = new \Think\Verify();// 开启验证码背景图片功能 随机使用 ThinkPHP/Library/Think/Verify/bgs 目录下面的图片$Verify->useImgBg = true;$Verify->entry();
Copy after login

Chinese verification code

If you want to use Chinese verification Code, you can set:

$Verify = new \Think\Verify();// 验证码字体使用 ThinkPHP/Library/Think/Verify/zhttfs/simhei.ttf$Verify->useZh = true;$Verify->entry();
Copy after login

The display effect is as shown:

If it cannot be displayed normally, please confirm your ThinkPHP/Library/Think/Verify/zhttfs There are Chinese font files under the / directory.

If there is no Chinese font file in ThinkPHP/Library/Think/Verify/zhttfs/, you can download or copy the Chinese font file from C:\Windows\Fonts\. Note that the font file extension is .ttf .

Verification code detection

You can use the check method of the Think\Verify class to check whether the input of the verification code is correct. For example, the following is an encapsulated function for verification code detection. :

// 检测输入的验证码是否正确,$code为用户输入的验证码字符串function check_verify($code, $id = ''){$verify = new \Think\Verify();return $verify->check($code, $id);
}
Copy after login

Exercise: Take login as an example:

Controller method:

<?php
namespace Home\Controller;use Think\Controller;class LoginController extends Controller
{    function Login()
    {        if(empty($_POST))
        {            $this->display();    
        }    
        else
        {            $code = $_POST["yzm"];             $verify = new \Think\Verify();           if($verify->check($code,2)) //code是用户输入的值 ,2是验证码检测标示,必须与生成的验证码标示相同才能验证            {                if($_POST["uid"]!="" && $_POST["pwd"]!="")
                {                    $model = D("Users");                    $uid = $_POST["uid"];    
                    $pwd = $_POST["pwd"];                    $attr = $model->field("Pwd")->find($uid);                    //var_dump($attr);
                    if($pwd==$attr["pwd"])
                    {
                        session("uid",$uid);   // 跳转页面之前将$uid存入session                    $this->success("登录成功!","Main");                                            }                    else
                    {                        $this->error("登录失败!");    
                    }
                }                else
                    {                        $this->error("用户名或者密码不能为空!");    
                    }
            }            else
            {                $this->error("验证码不正确!");    
            }
        }        
    }//生成验证码的操作方法
    function yzm()
    {        $config =    array(    
            'fontSize'    =>    30,    // 验证码字体大小    
            'length'      =>    4,     // 验证码位数  
            'useCurve'    =>    true,  // 是否画混淆曲线  
            'useNoise'    =>    true,  // 关闭验证码杂点
            'expire'      =>    60,    // 验证码有效期(秒)
            'useImgBg'    =>    false,  // 使用背景图片
            'useZh'       =>    true,  // 使用中文验证码
            'imageW'      =>    240,   // 验证码宽度
            'imageH'      =>    60,    // 验证码高度
            'fontttf'     =>    'simhei.ttf', // 验证码字体        );        $Verify = new \Think\Verify($config);        $Verify->entry(2);//参数是生成验证码的标示,适用于同一个页面有多个验证码的时候,生成验证码的标示必须与检测验证码的标示相同,否则验证码不正确    }?>
Copy after login

Request Login method:

The above is the detailed content of Detailed explanation of verification code of ThinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1258
29
C# Tutorial
1232
24
How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

See all articles