Home > php教程 > php手册 > Simeng PHP-Alibaba Dayu mobile phone verification code

Simeng PHP-Alibaba Dayu mobile phone verification code

WBOY
Release: 2016-10-22 00:00:31
Original
1553 people have browsed it

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!

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