Si Meng PHP-Ali Big Fish mobile phone verification code

Si Meng PHP-Ali Big Fish mobile phone verification code

Have you ever encountered a function that registered users need to use SMS verification when building a PC website? Or retrieve the password, and verify the user's information and other functions! Today, Si Meng PHP brings you the ability of ThinkPHP to integrate Ali Big Fish’s SMS verification!

First of all, we need to understand the principle of SMS sending. In fact, the third party we use is a communication SMS protocol, and then we send a randomly generated verification code to the user's mobile phone, and then we send it and save it in the database. The corresponding information is used to verify, the principle is like this, we will start to teach everyone to play this function step by step!

(1) We are going to build a data table to save SMS messages

(2) We are going to use third-party class files. Of course, you can download this class file from the platform. There is a PHP demo on it, and then we can integrate it into thinkphp. Add it to the class library and don't forget to declare the namespace!

(3) We configure some auxiliary variables in the configuration file

(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();
//There can only be 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 upper limit!";
}else {
   //The value of the verification code sent randomly
    $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 phone number, and the second is the id of the template, which is available in the application management
      A template management, the third is the random verification code we send, the first
      4.are order numbers, don’t fill 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 to receive the SMS, and when the user submits it, we will check the relevant verification code according to the user's mobile phone number!

Another thing that friends should pay attention to is that users who are not authenticated by Ali Dayu can only send text messages to users on the whitelist, so you have to configure the whitelist to ensure the success of our sending! In addition, when you first applied for it, there was a free 100-use number for us to test and use!

Reference: https://cloud.tencent.com/developer/article/1055986 Si Meng PHP-Ali Big Fish mobile phone verification code-Cloud + Community-Tencent Cloud