SMS.CN的短信接口
发送验证码
HTML index.php
<!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=utf-8" />
<title>短信模块</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
font-family: 微软雅黑;
}
.number{ height:22px; line-height:22px;font-family: 微软雅黑;}
-->
</style>
</head>
<body>
<!--以下功能是根据需要调用 A-->
<script language="JavaScript" type="text/javascript" src="js_yz.js"></script>
<form id="feedbackform" name="feedbackform" action="" method="post" >
<table width="501" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" style="margin-top:50px;">
<tr>
<td width="133" height="45" bgcolor="#FAFAFA" style="padding-left:10px;">获取短信验证码:</td>
<td width="365" height="45" bgcolor="#FAFAFA" style="padding-left:10px;"><input name="scms_gettel" id="scms_gettel" class="number" type="text" value="18810306233" />
<input name="right_yzcode" id="right_yzcode" type="hidden" value="" /> <input name="Submitbfs" type="button" value="获取验证码" class="btn submit" id="Submitbfs" onclick="giveduanxin(document.getElementById('scms_gettel').value)" style="height:28px;"/></td>
</tr>
<tr>
<td height="45" bgcolor="#FAFAFA" style="padding-left:10px;">您收到的验证码:</td>
<td height="45" bgcolor="#FAFAFA" style="padding-left:10px;"><input type="text" name="scms_yzcode" id="scms_yzcode" class="number" value="" maxlength="6" onblur="giveyz(document.getElementById('scms_yzcode').value)" style="float:left" /><div id="show_statu" style=" float:left;font-size:12px; text-align:left; border:1px solid #FFCC00; line-height:25px; background:#FFFFCC; color:#333333; display:none; height:25px; padding-left:10px; margin-left:8px;"></div></td>
</tr>
</table>
</form>
<!--以下功能是根据需要调用 B-->
</body>
</html>
* yz.js*
function qingkong(){ //清空提示函数
updateinfo();
}
var wait = 90; //停留时间
function updateinfo(){
if(wait == 0){
document.getElementById('Submitbfs').value = "获取验证码";
document.getElementById('Submitbfs').disabled="";
document.getElementById("show_statu").style.display='none';
//document.getElementById("show_statu").innerHTML="<span style=color:blue>如果您没有收到短信校验码,您现在可以重新获取!</span>";
wait=90 //还原重发时的初始值
}
else{
document.getElementById('Submitbfs').disabled="disabled"; //防止关闭层后,又激活了
document.getElementById('Submitbfs').value = "等待 "+wait+" 秒";
wait--;
setTimeout("updateinfo()",1000);
}
}
//------------------------------------------------------------
function giveyz(scms_yzcode)
{
var chk=true;
var divid=document.getElementById("show_statu");
var regex=/[0-9]$/
divid.style.display='block';
if (scms_yzcode=="" ){
divid.innerHTML="<span style='color:red'>请填写您收到的短信校验码!</span>";
}else if ( document.getElementById('right_yzcode').value!=scms_yzcode ){
divid.innerHTML="<span style='color:red'>您填写的短信校验码不正确!</span>";
}else if ( document.getElementById('right_yzcode').value==scms_yzcode ){
divid.innerHTML="<span style='color:green'>验证成功!</span>";
//divid.style.display='none';
}
}
var xmlHttp
function giveduanxin(tel)
{
var chk=true;
var divid=document.getElementById("show_statu");
var regex=/^(?:13\d|15\d|18[123456789])-?\d{5}(\d{3}|\*{3})$/;
divid.style.display='';
if (tel=="" ){
divid.innerHTML="<span style='color:red'>请填写手机号码!</span>";
}else if ( !regex.exec(tel) ){
divid.innerHTML="<span style='color:red'>手机号码格式不正确!</span>";
}else{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("抱歉,浏览器不支持")
return
}
var url="http://localhost/mcode/yz_send.php"
url=url+"?action=chk&tel="+tel
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
var give_strs= new Array(); //定义一数组
give_strs=xmlHttp.responseText.split("|"); //字符分割
if (give_strs[0]=="right"){
//document.getElementById('Submitbfs').style.visibility='hidden';
//closeWindow();
divid.innerHTML="<span style='color:green'>验证码已发送,请查收!</span>";
//document.getElementById('codeshows').innerHTML=yzm;
document.getElementById('Submitbfs').disabled="disabled";//立即失效,并开始提示下面是2秒后换提示内容并开始倒数
document.getElementById('right_yzcode').value=give_strs[1];//回传发到短信的校验码
setTimeout("qingkong()",2000);//1秒后提示,重新发送
}else{
divid.innerHTML=xmlHttp.responseText;
}
}
}
//document.getElementById('Submitbfs').disabled="disabled";
}
function GetXmlHttpObject(){
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
yz_send.php
<?php
$action=$_REQUEST['action'];
$tel=$_REQUEST['tel'];
$yzm=$_REQUEST['yzm'];
function randomkeys($length){
$pattern='1234567890'; //字符池
for($i=0;$i<$length;$i++){
$key.=$pattern{mt_rand(0,9)};//生成php随机数
}
return $key;
}
function give_ms($yzm,$tel){ //发送短信 短信开关,帐号,密码,内容,手机呈
file_get_contents("http://api.sms.cn/mt/?uid=xxxxx&pwd=".md5(xxxxxxx)."&mobile=".$tel."&mobileids=&content=".$msg."");
}
$yzm=randomkeys(6);
if ($action=="chk"){
if ($tel=="" || $yzm=="" ){
$instrs = "短信验证码发送失败!";
$outstrs = iconv('GBK','UTF-8',$instrs);
echo $outstrs;
exit;
}
//查询号码是否预约过
if($tel == true){
//查询数据库 leave_tell
$db = require('../caches/configs/database.php');
$con_arr = $db['default'];
$conn=mysql_connect($con_arr['hostname'],$con_arr['username'],$con_arr['password']) or die("error connecting") ; //连接数据库
mysql_query("set names 'utf8'");
mysql_select_db($con_arr['database']);
$tablename = $con_arr['tablepre'].'guestbook';
$sql ="select * from $tablename where leave_tell = ".$tel;
$result = mysql_query($sql,$conn);
$res = mysql_fetch_row($result);
if(!empty($res) && $res == true){
$instr = "您的手机号已经注册过!";
$outstr = iconv('GBK','UTF-8',$instr);
echo $outstr;
exit;
}
}
give_ms($yzm,$tel);
echo "right|".$yzm;
//echo "right|666666";
exit;
}
?>