search
Homephp教程php手册Simeng PHP-Alibaba Dayu mobile phone verification code

When you are building a PC website, have you ever encountered the function that registered users need to use SMS verification? Or retrieve passwords, verify user information, and more! Today, ThinkPHP will bring you the function of ThinkPHP integrating Alibaba Dayu SMS verification!
First of all, we need to understand the principle of sending SMS. In fact, the third party we use is a communication SMS protocol, and then a verification code we randomly generate is sent to the user’s mobile phone. After that, when we send it, we need it in the database. Save the corresponding information for verification. This is the principle. Next, we will start to teach you how to play this function step by step!
(1) We are going to build a data table to save SMS messages
Simeng PHP-Alibaba Dayu mobile phone verification code
(2) We are going to use a third-party class file. Of course, you can download this class file from the platform. There is a demo of PHP on it. Then we can integrate it into thinkphp. Don’t forget to declare the namespace when adding it to the class library!
(3) We configure some auxiliary variables in the configuration file
Simeng PHP-Alibaba Dayu mobile phone verification code
(4) This step is when we start to write logical operations
$_POST['tel'] = "18522713XXX";
$tel = $_POST['tel'];
$w['tel'] = $tel;
$res1 = D("Record")->where($w)->find();
//You can only have three chances in a day (verification)
$gap = time()-$res1['ctime'];
if($gap>=86400){
$data['time'] = 0;
D("Record")->where($w)->save($data);
}
$res1 = D("Record")->where($w)->find();
if($res1['time']>=3){
echo "SMS limit!";
}else {
//The value of the randomly sent verification code
$num = rand(1000, 9999);
//Instantiate an object
$ecd = new Ecd(C('url'), C('app_key'),
C('app_secret'), C('format'));
//Send verification code SMS
//The first parameter is the mobile phone number, the second is the template id, which is available in the application management
The management of a template, the third one is the random verification code we send, the third one is
Four are the order number, but you don’t need to fill it in
$res = $ecd->send_sms_code("$tel", '1', "$num", '');
$arr = json_decode($res, true);
if ($arr['result'] == 0 && $arr['msg'] == "Success") {
$w['tel'] = $tel;
$res1 = D("Record")->where($w)->find();
if ($res1) {
$data['vc'] = $num;
$data['time'] = $res1['time'] + 1;
$data['ctime'] = time();
$res2 = D("Record")->where($w)->save($data);
if ($res2) {
echo "sent successfully";
} else {
echo "Failed to send";
}
} else {
$data['tel'] = $tel;
$data['vc'] = $num;
$data['time'] = 1;
$data['ctime'] = time();
$res3 = D("Record")->add($data);
if ($res3) {
echo "sent successfully";
} else {
echo "Failed to send";
}
}
}
}
(5) That is, after receiving the text message, when the user submits, we will check the relevant verification code based on the user's mobile phone number!

Another thing that friends should pay attention to is that unauthenticated users of Alibaba Dayu can only send text messages to users in the whitelist, so you need to configure the whitelist to ensure the success of our sending! Also, when you first apply, there are 100 free items for us to test!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment