php method to implement login verification code: first generate a 4 to 6-digit random verification code; then save each generated character to the session or database; then send the verification code to the user's mobile phone; finally Just compare it with the entered verification code for verification.
Recommended: "PHP Video Tutorial"
PHP implements a simple verification code function mechanism
Website security is an issue that developers cannot ignore. Currently, one of the most commonly used methods to improve website security is to use the verification code function mechanism. Some only use a few digits and letters to create confusion. For verification codes, some use mobile phones to send text messages for verification, and some use email addresses for verification. But how is this verification code function mechanism implemented? The following is a detailed explanation of the implementation ideas and simple implementation methods of the verification code function mechanism.
1. Ideas for implementing the verification code function mechanism
① Conventional verification code implementation:
a. Generate a png picture
b. Set the background color of the picture
c, set the font color and style
d, generate a 4-digit random verification code
e, and adjust and rotate each generated character Draw the angle and position on the png image
f, add noise and interference lines to prevent the registration machine from analyzing the original image for malicious registration
g, output the image
h, and release the image Memory occupied
i. Save the verification code to the session or database
j. Compare it with the entered verification code
② SMS (email) verification code mechanism :
a. Generate a 4-6 digit random verification code
b. Save each generated character to the session or database
c. Verify The code is sent to the user's mobile phone (email)
d, the user enters it within the specified time
e, and the verification code is taken out from the session or database
f, Compare and verify with the entered verification code
2. Simple mechanism to implement the verification code function
① Create a new captcha.php and write the following code
<?php /** * ======================================= * Created by WeiBang Technology. * User: Wei ZhiHua * Date: 2016/10/12 0020 * Time: 下午 4:14 * Power: 实现验证码功能 * ======================================= */ //开启session session_start(); //创建一个大小为 100*30 的验证码 $image = imagecreatetruecolor(100, 30); $bgcolor = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bgcolor); $captch_code = ''; for ($i = 0; $i < 4; $i++) { $fontsize = 6; $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120)); $data = 'abcdefghijkmnpqrstuvwxy3456789'; $fontcontent = substr($data, rand(0, strlen($data) - 1), 1); $captch_code .= $fontcontent; $x = ($i * 100 / 4) + rand(5, 10); $y = rand(5, 10); imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } //就生成的验证码保存到session $_SESSION['authcode'] = $captch_code; //在图片上增加点干扰元素 for ($i = 0; $i < 200; $i++) { $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200)); imagesetpixel($image, rand(1, 99), rand(1, 29), $pointcolor); } //在图片上增加线干扰元素 for ($i = 0; $i < 3; $i++) { $linecolor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220)); imageline($image, rand(1, 99), rand(1, 29), rand(1, 99), rand(1, 29), $linecolor); } //设置头 header('content-type:image/png'); imagepng($image); imagedestroy($image); ?>
② Create a new form .php, write the following code
<?php /** * ======================================= * Created by WeiBang Technology. * User: Wei ZhiHua * Date: 2016/10/12 0021 * Time: 下午 4:14 * Power: 实现验证码功能 * ======================================= */ if (isset($_REQUEST['authcode'])) { session_start(); if (strtolower($_REQUEST['authcode']) == $_SESSION['authcode']) { echo "输入正确!"; } else { echo "输入错误!"; } exit(); } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <title>确认验证码</title> </head> <body> <form method="post" action="./form.php"> <p>验证码图片: <img id="captcha_img" border="1" src="./captcha.php?r=<?php echo rand(); ?>" width=100 height=30> <a href="javascript:void(0)" onClick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()">换一个?</a> </p> <p>请输入图片中的内容:<input type="text" name="authcode" value=""/></p> <p><input type="submit" value="提交" style="padding:6px 20px;"></p> </form> </body> </html>
The above is the production idea and implementation method of PHP verification code, from simple to complex, you can write a perfect verification code function mechanism based on these.
The above is the detailed content of How to implement login verification code in php. For more information, please follow other related articles on the PHP Chinese website!