Home>Article>Backend Development> PHP generates graphic verification code (enhanced interference type)

PHP generates graphic verification code (enhanced interference type)

安安杰尼
安安杰尼 Original
2020-04-30 16:05:58 195353browse

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

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

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), '*', $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

       

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!

Statement:
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