短信验证码注册,很简单,用的是 云通讯的短信系统(收费的,不过有测试的api给我们做测试)。好了,不多说,进入正题。
1、收到到云通讯短信系统注册账号,然后下载他们的封装好的短信api接口代码,解压,然后找到CCPRestSDK.php文件和SendTemplateSMS.php文件,将其拉到根目录文件夹里。
2、打开SendTemplateSMS.php文件,首先注意include_once('./CCPRestSDK.php'),千万别包含错路径了,将云通讯给的测试主账号,主账号Token,应用ID填到这三个变量中。
3、创建sendMessage.html页面(此处也没做过多的安全过滤)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<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、输入手机号 点击免费发送验证码 ,查看发送成功了
5、在SendTemplateSMS.php文件里已经写好了手机号然后进行处理的代码
1
2
3
4
5
6
7
8
|
<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、如果不出意外,手机会收到四位数字的验证码,然后输入验证码,将其提交到reg.php文件
7、reg.php文件进行验证码正确与否进行逻辑验证*(我这只是做实验,所以没做多少安全过滤处理)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<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、结果输出验证码正确
整个验证码验证逻辑就这么简单,希望能够帮助大家顺利的完成短信验证码验证实现注册功能。