1.表单传输页面
<?php session_start(); error_reporting(E_ALL & ~E_NOTICE); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>手机验证码</title> <? $action=$_GET["action"]; if ($action==‘add‘) { if ((strtotime($_SESSION[‘time‘]) + 60) < time()) {//将获取的缓存时间转换成时间戳加上60秒后与当前时间比较,小于当前时间即为过期 session_destroy(); unset($_SESSION); //header(‘content-type:text/html; charset=utf-8;‘); echo ‘<script>alert("验证码已过期,请重新获取!");</script>‘; } else { $Code = trim($_POST["TextBox2"]); if (strlen($Code) > 1 and $Code == $_SESSION[‘rand‘]) print("<script>alert(‘验证成功‘)</script>"); else print("<script>alert(‘验证失败‘)</script>"); unset($_SESSION[‘rand‘]); } } ?> <script src="js/jquery.js" language="javascript" type="text/javascript"></script> <script type="text/javascript"> function get(obj) { var partten = /^\d{10,13}$/; if (!partten.test(document.getElementById("TextBox1").value)) { alert(‘请输入正确的手机号码‘); return; } obj.disabled = true; $.ajax({ url: "getcode.php", type: "Post", data: "Tel=" + $("#TextBox1").val(), success: function(msg) { obj.disabled = false; if (msg == "ok") { alert("111") return; } if (msg == "error") { alert("22") return; } alert(msg); } }) } </script> </head> <body> <label> <form id="form1" name="form1" method="post" action="index.php?action=add"> 您手机号码: <input name="TextBox1" type="text" id="TextBox1" /> <input id="Button1" type="button" value="获取短信验证码" onClick="get(this)" /> <br /> <br /> 验证码: <input name="TextBox2" type="text" id="TextBox2" /> </label> <br /> <br /> <label> <input type="submit" name="Submit" value=" 确 定 " /> </label> </form> <p> </p> </body> </html>
2.数据处理页面,(发送短信验证)
<?php session_start(); include "TopSdk.php"; date_default_timezone_set(‘Asia/Shanghai‘); //生成随机验证码 $srand = rand(1000,9999); $_SESSION[‘rand‘]=$srand; //生成缓存时间 if (isset($_SESSION[‘time‘]))//判断缓存时间 { $_SESSION[‘time‘]; } else { $_SESSION[‘time‘] = date("Y-m-d H:i:s"); } $rand = $_SESSION[‘rand‘]; $c = new TopClient; $appkey = ‘XXX‘;//你的appkey $secret = ‘XXXX‘;//你的secret //$phone = ‘XXXXXX‘; $phone=$_POST["Tel"];//得到的手机号 $c->appkey = $appkey; $c->secretKey = $secret; $req = new AlibabaAliqinFcSmsNumSendRequest; $req->setExtend(""); $req->setSmsType("normal"); $req->setSmsFreeSignName("XX");//你的短信验证签名 $req->setSmsParam("{number:‘$rand‘}"); $req->setRecNum($phone); $req->setSmsTemplateCode("XX");//你的短信模板ID $resp = $c->execute($req); ?>
原文地址:http://zhangzhichao.blog.51cto.com/11126669/1834374