thinkphp is a popular PHP framework for developing web applications. When using the thinkphp framework, verification codes are often needed to increase system security. The default verification code styles and images of thinkphp may not be suitable for certain application scenarios and need to be changed. This article will introduce how to change the default verification code of thinkphp.
1. Find the verification code controller
In the thinkphp framework, the verification code is generated and output by a built-in controller. The source code of this controller can be found in the library file directory of the thinkphp framework. The general path is: thinkphp/library/think/captcha/Controller.php.
2. Copy the verification code controller
In order to avoid errors during system updates caused by modifying the default verification code controller, you should copy the default verification code controller to a new location in the application directory. in the directory.
The specific steps are as follows:
- Create a new directory, for example: \application\extra\captcha
- Copy the controller Controller.php to this directory.
- Modify the namespace of Controller.php to: namespace app\extra\captcha;
3. Modify the verification code controller
Now we will You can modify the verification code controller to change the generation and output methods of verification codes.
- Modify the output method
The default output method of the verification code is to return a picture in gif format, and we can modify the output method to implement other styles of verification codes. For example, the verification code can be output as an image in SVG format.
Modify the verification code output method as follows:
public function show() { $config = [ 'expire' => 3, //验证码过期时间(s) 'length' => 4, //验证码长度 'imageH' => 60, //验证码高度 'fontSize' => 20, //验证码字体大小(px) 'useCurve' => false, //是否画混淆曲线 'useNoise' => false, //是否添加杂点 'bg' => [255, 255, 255], //背景颜色 ]; $captcha = new Captcha($config); $captcha->codeSet = '0123456789';//只用数字作为验证码 // 输出svg格式的图片 echo $captcha->entry('svg'); }
- Modify the verification code generation method
thinkphp’s default verification code generation method is to randomly generate numbers and letters , and we can also modify the generation method to meet different needs. For example, a verification code in Chinese characters can be generated.
Modify the verification code generation method as follows (taking Chinese verification code generation as an example):
public function show() { //引入新字体文件(这里以方正胖头鱼体为例,字体文件需要自己上传) $ttf_file = __DIR__ .'/fzpty.ttf'; $codeSet = '玩家参与游戏,从而培养了自己 的游戏能力'; //验证码文字内容 $config = [ 'expire' => 3, //验证码过期时间(s) 'length' => 4, //验证码长度 'useZh' => true, //使用中文验证码 'fontttf' => $ttf_file, //指定ttf字体文件 'imageH' => 60, //验证码高度 'fontSize' => 20, //验证码字体大小(px) 'useCurve' => false, //是否画混淆曲线 'useNoise' => false, //是否添加杂点 'bg' => [255, 255, 255], //背景颜色 ]; $captcha = new Captcha($config); $captcha->codeSet = $codeSet;//设置验证码文字内容 // 输出svg格式的图片 echo $captcha->entry('svg'); }
4. Modify the verification code configuration file
thinkphp verification code controller will read The parameters in the configuration file determine how the verification code is generated and output. Therefore, we can also modify the configuration file to achieve the purpose of changing the verification code.
The specific steps are as follows:
- Copy the framework's default verification code configuration file to your own application directory
Open the thinkphp framework's verification code configuration file config. php, copy it to the config directory under the application directory, and rename it to captcha.php.
- Modify the verification code configuration
You can modify the related configuration parameters of the verification code in captcha.php. For example, you can modify the length, expiration time, font size and other parameters of the verification code:
return [ 'useNoise' => false, // 是否添加杂点 'useCurve' => false, // 是否画混淆曲线 'fontSize' => 25, // 验证码字体大小 'expire' => 1800, // 验证码过期时间(s) 'length' => 4, // 验证码长度 'fontttf' => '', //验证码字体文件路径 'bg' => [255, 255, 255], // 验证码背景颜色,如果设置为数组则表示渐变色 ];
5. Summary
Through the above steps, we can modify thinkphp’s default verification code to achieve customization Defined styles and verification code content. It should be noted that when modifying the verification code controller and configuration files, the copied files must be placed in the application directory to avoid overwriting after version updates.
The above is the detailed content of How to change thinkphp default verification code. For more information, please follow other related articles on the PHP Chinese website!

This article demonstrates building command-line applications (CLIs) using ThinkPHP's CLI capabilities. It emphasizes best practices like modular design, dependency injection, and robust error handling, while highlighting common pitfalls such as insu

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

The article discusses preventing SQL injection vulnerabilities in ThinkPHP through parameterized queries, avoiding raw SQL, using ORM, regular updates, and proper error handling. It also covers best practices for securing database queries and validat

The article discusses key differences between ThinkPHP 5 and 6, focusing on architecture, features, performance, and suitability for legacy upgrades. ThinkPHP 5 is recommended for traditional projects and legacy systems, while ThinkPHP 6 suits new pr

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

The article discusses best practices for handling file uploads and integrating cloud storage in ThinkPHP, focusing on security, efficiency, and scalability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
