PHP implements automatic expiration verification of SMS verification code

PHPz
Release: 2023-09-13 09:20:02
Original
730 people have browsed it

PHP implements automatic expiration verification of SMS verification code

PHP implements automatic expiration verification of SMS verification codes

With the development of Internet technology, SMS verification codes have become a commonly used security method in important operations such as user registration and login. means of verification. The validity period of the SMS verification code is a very important security measure. Expiration verification can ensure the validity of the verification code and protect the user's privacy and account security. This article will introduce how to use PHP language to implement automatic expiration verification of SMS verification codes, and give specific code examples.

  1. Design database table structure

First, we need to design a data table to store information related to SMS verification codes. This information includes mobile phone number, verification code, generation time and expiration time, etc. Here is a simple design example:

CREATE TABLEsms_verification(
idint(11) NOT NULL AUTO_INCREMENT,
phonevarchar(20) NOT NULL,
codevarchar(6) NOT NULL,
generate_timeint(11) NOT NULL,
expiry_timeint(11) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  1. Generate and send SMS verification Code

When the user requests to send an SMS verification code, we need to generate a random verification code and store it in the database. At the same time, we need to record the generation time and expiration time of the verification code. The following is a sample function:

function sendSMSVerificationCode($phone) {

// 生成随机的验证码 $code = rand(100000, 999999); $generateTime = time(); $expiryTime = $generateTime + 600; // 验证码有效期为10分钟 // 将验证码信息存入数据库 $sql = "INSERT INTO sms_verification (phone, code, generate_time, expiry_time) VALUES ('$phone', '$code', '$generateTime', '$expiryTime')"; // 执行插入操作,这里假设使用PDO预处理语句来防止SQL注入 // ... // 调用短信接口发送验证码 // ... // 返回生成的验证码 return $code;
Copy after login

}

  1. Verification SMS verification code

When When a user submits a verification code for login or registration, we need to verify whether the verification code is valid. At the same time, we also need to check whether the verification code has expired. The following is a sample function:

function verifySMSVerificationCode($phone, $code) {

// 查询数据库中的验证码信息 $sql = "SELECT * FROM sms_verification WHERE phone='$phone' ORDER BY generate_time DESC LIMIT 1"; // 执行查询操作,这里假设使用PDO预处理语句来防止SQL注入 // ... // 获取查询结果 $verificationInfo = $stmt->fetch(PDO::FETCH_ASSOC); // 如果查询结果为空,则验证码无效 if (!$verificationInfo) { return false; } // 检查验证码是否已经过期 if (time() > $verificationInfo['expiry_time']) { return false; } // 检查验证码是否匹配 if ($code != $verificationInfo['code']) { return false; } // 验证通过,删除数据库中的验证码信息 $sql = "DELETE FROM sms_verification WHERE id={$verificationInfo['id']}"; // 执行删除操作 // ... return true;
Copy after login

}

Through the above code example, we realize the automatic expiration of SMS verification code Verification function. When sending the verification code, we generate a random verification code and store it in the database. At the same time, we will also record the generation time and expiration time of the verification code. When a user submits a verification code for verification, we query the verification code information from the database and check whether the verification code has expired and matched.

It should be noted that in actual applications, we also need to handle some abnormal situations, such as database errors, failure to send text messages, etc. In addition, in order to improve security, we can also add some additional verification mechanisms, such as SMS sending frequency limits, verification code error limit, etc.

To sum up, implementing automatic expiration verification of SMS verification codes through PHP is a relatively simple operation. By properly designing the database table structure and using appropriate code logic, we can provide a more secure and reliable verification code verification function to protect user account security and privacy.

The above is the detailed content of PHP implements automatic expiration verification of SMS verification code. For more information, please follow other related articles on the PHP Chinese website!

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!