php method to implement verification code login: 1. Create an html page to display the verification code; 2. Create a php file and accept the verification code information passed in from the front-end page; 3. Create a verification code to generate a PHP file.
The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.
Verification codes are very common in our daily lives. Using verification codes has many benefits, such as:
Prevent malicious cracking of passwords. For example, in order to obtain user information, some hackers use different means to send data to the server to verify the accuracy of guessed user information.
Prevent malicious ticket manipulation and forum flooding. This phenomenon of flooding has existed in forums for a long time, such as Tieba, where posts are constantly sent. Now Baidu Tieba has set a time limit for users to post. If there are too many posts within a certain period of time, you will not be able to continue posting.
Prevent malicious requests. For example, if a user submits a form information, it will cause a waste of server resources and malicious attacks by continuously requesting data information from the background.
Fun. This is a personal opinion. When we submit a form, if we encounter some interesting verification code methods, it can enhance the user's love for the website.
Get user information. This is a common occurrence in today's sites. For example, if we register an account on a site, we need to pass the mobile phone verification code to register successfully. After getting the user's phone number, the website will send some marketing information to the owner of the mobile phone number.
So how do we implement the verification code login function? Let’s take a look together!
Environment configuration:
You only need to install the GD extension library of PHP locally to use it. How to check, create a PHP file and write in the file, access the file, and if you see that the GD library has been installed, you can use it.
Specific code implementation:
1. We first create an html page to display the verification code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登陆</title> </head> <body> <form action="./login.php" method="post"> <img src="image_captcha.php" onclick="this.src='image_captcha.php?'+new Date().getTime();" width="200" height="200"><br/> <input type="text" name="captcha" placeholder="请输入图片中的验证码"><br/> <input type="submit" value="验证"> </form> </body> </html>
2. Create a PHP file to accept the verification code information passed in from the front-end page.
<?php /** * 接受用户登陆时提交的验证码 */ session_start(); //1. 获取到用户提交的验证码 $captcha = $_POST["captcha"]; //2. 将session中的验证码和用户提交的验证码进行核对,当成功时提示验证码正确,并销毁之前的session值,不成功则重新提交 if(strtolower($_SESSION["captchaimg"]) == strtolower($captcha)){ echo "验证码正确!"; $_SESSION["captcha"] = ""; }else{ echo "验证码提交不正确!"; }
3. Create verification code to generate PHP file
<?php /** * 字母+数字的验证码生成 */ // 开启session session_start(); //1.创建黑色画布 $image = imagecreatetruecolor(100, 30); //2.为画布定义(背景)颜色 $bgcolor = imagecolorallocate($image, 255, 255, 255); //3.填充颜色 imagefill($image, 0, 0, $bgcolor); // 4.设置验证码内容 //4.1 定义验证码的内容 $content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; //4.1 创建一个变量存储产生的验证码数据,便于用户提交核对 $captcha = ""; for ($i = 0; $i < 4; $i++) { // 字体大小 $fontsize = 10; // 字体颜色 $fontcolor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); // 设置字体内容 $fontcontent = substr($content, mt_rand(0, strlen($content)), 1); $captcha .= $fontcontent; // 显示的坐标 $x = ($i * 100 / 4) + mt_rand(5, 10); $y = mt_rand(5, 10); // 填充内容到画布中 imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } $_SESSION["captcha"] = $captcha; //4.3 设置背景干扰元素 for ($$i = 0; $i < 200; $i++) { $pointcolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200)); imagesetpixel($image, mt_rand(1, 99), mt_rand(1, 29), $pointcolor); } //4.4 设置干扰线 for ($i = 0; $i < 3; $i++) { $linecolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200)); imageline($image, mt_rand(1, 99), mt_rand(1, 29), mt_rand(1, 99), mt_rand(1, 29), $linecolor); } //5.向浏览器输出图片头信息 header('content-type:image/png'); //6.输出图片到浏览器 imagepng($image); //7.销毁图片 imagedestroy($image);
Recommended learning: php training
The above is the detailed content of How to implement verification code login in php. For more information, please follow other related articles on the PHP Chinese website!