Home  >  Article  >  Backend Development  >  How to implement SMS verification in php

How to implement SMS verification in php

藏色散人
藏色散人Original
2022-11-07 09:34:102290browse

php method to implement SMS verification: 1. Register an account and download the interface; 2. Configure parameter variables; 3. Call the interface to test SMS; 4. Pass the code "if(strtolower($_SESSION["captcha"] ) == strtolower($captcha)){echo $_POST["captcha"]."...";$_SESSION["captcha"] = "";if(...)..." Just implement verification .

How to implement SMS verification in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to implement SMS verification in php?

php realizes the whole process of mobile phone SMS verification code

In our work and life, when using various websites and APPs, regardless of registration, payment, or changing passwords, etc. Everyone is required to obtain an SMS verification code for verification, so how is the verification code sent? Next, taking Huaxing Cloud SMS as an example (only as an example, mainly to understand the implementation ideas), I will introduce in detail the implementation process of sending SMS verification codes in PHP (while realizing the anti-swiping SMS verification code mechanism and encrypted communication).

1. Implementation ideas

How to implement SMS verification in php

##2. Implementation process

1. Register account

Register Huaxing Cloud SMS account , fill in the basic information, and a dedicated person will contact you to open an account, and a small number of text messages will be sent in advance for testing.

2. Download interface

Click "Product Center" to enter the product page, select the corresponding PHP secure cloud SMS interface and HTTPS version cloud SMS interface document download; the downloaded document is as follows:

How to implement SMS verification in php

3. Configuration parameter variables

Configure the demo_https.php file:

(1) Change curlPost, curlGet, Copy the three sendSMS methods to the corresponding locations in the project. If you need to obtain the balance, copy the getBalance method at the same time.

(2) Modify the registration code ($reg_code), password ($reg_pw), signature ($signature), and text message content ($content) in the sendSMS method to your own information. Note: The content of text messages may need to be reported.

(3) Modify the target mobile phone number and content according to the comment rules in the code, and call the sendSMS method where you need to send text messages.

4. Reporting SMS Template

Submit the content to be sent to Huaxing customer service staff, who will check blacklists, sensitive words, etc. to complete the reporting work.

5. Call the interface to test SMS

Complete a series of preliminary preparations and actually test the SMS sending situation by calling the php cloud SMS interface.

3. Run the code

View the HTTPS version of the cloud SMS interface document to view the interface address, parameter description and examples. After calling the code below to configure the parameters, call this method to test sending SMS and print the return Value, you can check the error code in the document

image_captcha.php to generate the image verification code (anti-swipe mechanism)

<?php
/**
* 字母+数字的验证码生成
*/
// 开启session
session_start();
//1.创建黑色画布
$image = imagecreatetruecolor(100, 30);
//2.为画布定义(背景)颜色
$bgcolor = imagecolorallocate($image, 255, 255, 255);
//3.填充颜色
imagefill($image, 0, 0, $bgcolor);
// 4.设置验证码内容
//4.1 定义验证码的内容
$content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//4.1 创建一个变量存储产生的验证码数据,便于用户提交核对
$captcha = "";
for ($i = 0; $i < 4; $i++) {
// 字体大小
$fontsize = 10;
// 字体颜色
$fontcolor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
// 设置字体内容
$fontcontent = substr($content, mt_rand(0, strlen($content)), 1);
$captcha .= $fontcontent;
// 显示的坐标
$x = ($i * 100 / 4) + mt_rand(5, 10);
$y = mt_rand(5, 10);
// 填充内容到画布中
imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
$_SESSION["captcha"] = $captcha;
//4.3 设置背景干扰元素
for ($$i = 0; $i < 200; $i++) {
$pointcolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
imagesetpixel($image, mt_rand(1, 99), mt_rand(1, 29), $pointcolor);
}
//4.4 设置干扰线
for ($i = 0; $i < 3; $i++) {
$linecolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
imageline($image, mt_rand(1, 99), mt_rand(1, 29), mt_rand(1, 99), mt_rand(1, 29), $linecolor);
}
//5.向浏览器输出图片头信息
header(&#39;content-type:image/png&#39;);
//6.输出图片到浏览器
imagepng($image);
//7.销毁图片
//imagedestroy($image); 
index.html前端代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>短信验证码</title>
</head>
<body>
<form action="./sendsms.php" method="post">
<div>
手机号:<input id="phone" type="text" name="phone" value="" />
</div>
<div>验证码: <input type="text" name="captcha" placeholder="请输入图片中的验证码">
<img  src="image_captcha.php" onclick="this.src=&#39;image_captcha.php?&#39;+new Date().getTime();"    style="max-width:90%"  style="max-width:90%" alt="How to implement SMS verification in php" ><br/></div>
<div> <input type="submit" value="验证"></div>
</form>
</body>
</html>

sendsms.php back-end running code:

<?php
/**
* 接受用户登陆时提交的验证码
*/
session_start();
//1. 获取到用户提交的验证码、手机号
$phone = $_POST[&#39;phone&#39;];//手机号
$captcha = $_POST["captcha"];
//2. 将session中的验证码和用户提交的验证码进行核对,当成功时提示验证码正确,并销毁之前的session值,不成功则重新提交
if(strtolower($_SESSION["captcha"]) == strtolower($captcha)){
echo $_POST["captcha"]."验证码正确!";
$_SESSION["captcha"] = "";
if(preg_match_all("/^1[34578]\d{9}$/", $phone)) { 
//得到手机号 
$url = &#39;https://www.stongnet.com/sdkhttp/sendsms.aspx&#39;;
$reg_code = &#39;101100-WEB-HUAX-111111&#39;; //华兴软通注册码,请在这里填写您从客服那得到的注册码
$reg_pw = &#39;11111111&#39;; //华兴软通注册码对应的密码,请在这里填写您从客服那得到的密码
$source_add = &#39;&#39;; //子通道号(最长10位,可为空
//$phone = &#39;15891234567&#39;; //手机号码(最多1000个),多个用英文逗号(,)隔开,不可为空
$content = &#39;华兴软通验证码:&#39; . range(1000,9999);; //短信内容(含有中文,特殊符号等非ASCII码的字符,用户必须保证其为UTF-8编码格式)
$param_arry = array();
$param_arry[&#39;reg&#39;] = $reg_code;
$param_arry[&#39;pwd&#39;] = $reg_pw;
$param_arry[&#39;sourceadd&#39;] = $source_add;
$param_arry[&#39;phone&#39;] = $phone;
$param_arry[&#39;content&#39;] = $content;
$ca_info = dirname(__FILE__) . &#39;/cacert.pem&#39;; //根证书文件路径,相对路径和绝对路径均可,推荐使用绝对路径;demo里文件和源码放在一起了,为了安全证书文件最好不要和应用代码放在一起
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($param_array));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); //验证交换证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //检查SSL证书公用名是否存在,并且是否与提供的主机名匹配
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); //设置实现协议为TLS1.0版本
curl_setopt($ch, CURLOPT_CAINFO, $ca_info); 
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if(!empty($error)){ //curl有错误
echo $error;
}else{ //输出请求结果
echo $data;
}
}else{echo "请输入正确手机号";}
}else{
echo "验证码提交不正确!";
}

Follow the example You can use the registration number and password provided by customer service to test sending SMS verification codes to mobile phone numbers, and conduct further functional development according to your own requirements based on the interface return codes and documents.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to implement SMS verification in php. For more information, please follow other related articles on the PHP Chinese website!

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