Home > Article > Backend Development > PHP sends SMS verification code to complete the registration function, send SMS verification code_PHP tutorial
SMS verification code registration is very simple. It uses the SMS system of Yun Communication (chargeable, but there is The test API is used for testing by us). Okay, without further ado, let’s get to the point.
1. After receiving the registered account in the Cloud Communication SMS system, download their packaged SMS api interface code, unzip it, and then find the CCPRestSDK.php file and SendTemplateSMS.php file and pull it to the root folder.
2. Open the SendTemplateSMS.php file. First, pay attention to include_once('./CCPRestSDK.php'). Be sure not to include the wrong path. Fill in the test main account, main account Token, and application ID given by Cloud Communication here. among three variables.
3. Create the sendMessage.html page (no excessive security filtering is done here)
<span style="font-family: 'Microsoft YaHei'; font-size: 16px;"><!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Index</title> <script type="text/javascript" src="jquery.min.js"></script> </head> <body> <form action="reg.php" method="post"> 手机号:<input type="text" name="tel" id="tel"><br> 验证码:<input type="text" name="verify" id=""><span><button id="btn" type="button">免费发送验证码</button></span><br> <input type="submit" name="" value="注册"> </form> <script type="text/javascript"> $('#btn').click(function(){ var tel = $.trim($('#tel').val()); $.post('SendTemplateSMS.php', {'tel':tel},function(res){ if (res) { alert('发送成功'); } else { alert('发送失败'); } }); }); </script> </body> </html> </span>
4. Enter your mobile phone number and click to send the verification code for free, and check if the verification code has been sent successfully
5. The mobile phone number has been written in the SendTemplateSMS.php file and then processed
<span style="font-family: 'Microsoft YaHei'; font-size: 16px;"> $tel = $_POST['tel']; $res = sendTemplateSMS($tel, array($verify, 2),"1"); //$verify是所包含的verify.php文件里动态生成的四位数字验证码变量,生成时已将验证码存于SESSION里 ,到提交验证码时用于验证判断 if ($res) { echo '1'; } else { echo '0'; } </span>
6. If nothing goes wrong, the phone will receive a four-digit verification code, then enter the verification code and submit it to the reg.php file
7. Logically verify whether the verification code is correct in the reg.php file* (I am just doing an experiment, so I haven’t done much security filtering)
<span style="font-family: 'Microsoft YaHei'; font-size: 16px;"><?php session_start(); $tel = $_POST['tel']; $ver = trim($_POST['verify']); if ( $ver == $_SESSION['verify']) { echo '验证码正确'; } else { echo '验证码有误'; } ?> </span>
8. The result output verification code is correct
The entire verification code verification logic is so simple. I hope it can help everyone successfully complete the SMS verification code verification and realize the registration function.