


PHP generates graphic verification code (enhanced interference type)
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 = '/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']); } }
Verification
ob_clean(); $verify = new Code(); $verify->doimg();
This will output the following verification code
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), '*', $color); } }
. 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('VerifyCode') == strtolower($code)) { if($clear) $this->clearCode(); return true; } if($clear) $this->clearCode(); return false; } //清除验证码 public function clearCode() { Session::del('VerifyCode'); }
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
