PHP verification code class ValidateCode analysis

墨辰丷
Release: 2023-03-28 08:52:02
Original
1589 people have browsed it

This article mainly analyzes the PHP verification code class ValidateCode in detail, which has certain reference value. Interested friends can refer to it

PHP parsing verification code class

1. Start

I saw the ValidateCode written in PHP on the Internet to generate a verification code class. It felt good, so I used it to analyze and learn.

2. Class diagram

3. Verification code class part code

3.1 Define variables

//随机因子 private $charset = 'abcdefghjkmnprstuvwxyzABCDEFGJKMNPRSTUVWXYZ23456789'; private $code; private $codeLen = 4; private $width = 130; private $heigh = 50; private $img;//图像 private $font;//字体 private $fontsize = 20;
Copy after login

$charset is a random factor, here are a few that are not easy to distinguish. Characters, such as letters "i,l,o,q" and numbers "0,1". If necessary, you can add some Chinese or other characters or calculations, etc.

$codeLen indicates the length of the verification code, usually 4 digits.

3.2 Constructor, set the verification code font, and generate a true color image img

public function __construct() { $this->font = ROOT_PATH.'/font/Chowderhead.ttf'; $this->img = imagecreatetruecolor($this->width, $this->heigh); }
Copy after login

3.3 Randomly select 4 characters from the random factor as the $code verification code.

//生成随机码 private function createCode() { $_len = strlen($this->charset) - 1; for ($i = 0; $i < $this->codeLen; $i++) { $this->code .= $this->charset[mt_rand(0, $_len)]; } }
Copy after login

3.4 Generate the background color of the verification code.

//生成背景 private function createBg() { $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255)); imagefilledrectangle($this->img, 0, $this->heigh, $this->width, 0, $color); }
Copy after login

Among them mt_rand(157, 255), the purpose is to randomly select A lighter color.

3.5 Generate text on the image.

//生成文字 private function createFont() { $_x = $this->width / $this->codeLen; $_y = $this->heigh / 2; 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(3, 5), $_y + mt_rand(2, 4), $color, $this->font, $this->code[$i]); } }
Copy after login

Generate verification code on the image For text, the main consideration is the position of the text on the image and the color of each text.

Control the x-axis position of the nth text = (image width/verification code length) * (n-1) random offset number; where n = {d1....n}

Control the y-axis position of the nth text = image height/2 random offset number;

mt_rand(0, 156) randomly selects the text color, 0-156 aims to select a darker color .

mt_rand(-30, 30) Random text rotation.

3.6 Generate lines and snowflakes on the image

//生成线条,雪花 private function createLine() { for ($i = 0; $i < 15; $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->heigh), mt_rand(0, $this->width), mt_rand(0, $this->heigh), $color); } for ($i = 0; $i < 150; $i++) { $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->heigh), '#', $color); } }
Copy after login

When drawing lines, Choose a darker color value, and choose a lighter color value when drawing snowflakes. The purpose is to not affect the human eye's recognition of the verification code as much as possible, and to interfere with the automatic recognition code mechanism.

3.7 Generate verification code images externally for external calls.

//对外生成 public function doImg() { $this->createBg(); //1.创建验证码背景 $this->createCode(); //2.生成随机码 $this->createLine(); //3.生成线条和雪花 $this->createFont(); //4.生成文字 $this->outPut(); //5.输出验证码图像 }
Copy after login

3.8 Complete code:

         img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255)); imagefilledrectangle($this->img, 0, $this->heigh, $this->width, 0, $color); } //生成文字 private function createFont() { $_x = $this->width / $this->codeLen; $_y = $this->heigh / 2; 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(3, 5), $_y + mt_rand(2, 4), $color, $this->font, $this->code[$i]); } } //生成线条,雪花 private function createLine() { for ($i = 0; $i < 15; $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->heigh), mt_rand(0, $this->width), mt_rand(0, $this->heigh), $color); } for ($i = 0; $i < 150; $i++) { $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->heigh), '#', $color); } } //输出图像 private function outPut() { header('Content-Type: image/png'); imagepng($this->img); imagedestroy($this->img); } //对外生成 public function doImg() { $this->createBg(); //1.创建验证码背景 $this->createCode(); //2.生成随机码 $this->createLine(); //3.生成线条和雪花 $this->createFont(); //4.生成文字 $this->outPut(); //5.输出验证码图像 } //获取验证码 public function getCode() { return strtolower($this->code); } }
Copy after login

4. Test

Test code:

doImg();
Copy after login

Generate verification code:

5. Apply

##

Copy after login

The above onclick code is to click on the verification code image, which can automatically refresh the verification code.

code.php:

doImg(); $_SESSION['ValidateCode']=$_vc->getCode();
Copy after login

The complete code for the application can be downloaded from https://git.oschina.net/andywww/myTest Download CMS1.0 file.

6. Summary

During the independent testing process, no problems were found; but when applied to the project, it was initially found that the verification code image could not be generated. I searched online and some said that in the outPut() function,

added a line of ob_clean() code in front of header('Content-Type: image/png'); this line of code can solve the problem. Verification code issue. Although this method is simple, it may cause other problems with buffered data because the db_clean() function discards the contents of the output buffer.

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

Generate pictures with PHPVerification codeFunction details

php implements websiteVerification codeFunction

php encapsulatedVerification codeClass detailed explanation

The above is the detailed content of PHP verification code class ValidateCode analysis. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!