How to implement login verification code in php

藏色散人
Release: 2023-03-06 06:30:01
Original
4297 people have browsed it

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.

How to implement login verification code in php

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 = &#39;&#39;;
for ($i = 0; $i < 4; $i++) {
    $fontsize = 6;
    $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
    $data = &#39;abcdefghijkmnpqrstuvwxy3456789&#39;;
    $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[&#39;authcode&#39;] = $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(&#39;content-type:image/png&#39;);
imagepng($image);
imagedestroy($image);
 
?>
Copy after login

② 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[&#39;authcode&#39;])) {
    session_start();
    if (strtolower($_REQUEST[&#39;authcode&#39;]) == $_SESSION[&#39;authcode&#39;]) {
        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(&#39;captcha_img&#39;).src=&#39;./captcha.php?r=&#39;+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>
Copy after login

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!

Related labels:
php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template