thinkphp一个发送短信的接口问题

使用thinkphp写一个发送短信接口;遇到的一系列问题。

自己可以定几个规则(我定的规则是):

1、同一个电话号码5分钟内不能重发发送;

2、同一个电话号码一天不能超过五条;

3、配备电话号码的正确性(使用正则表达式:'/^[1]+[3,4,5,8,7]+\d{9}/');

如果为了防止有人无休止的刷你的短信接口;你可以再加一个加密;

php代码如下:

进入发送短信页面前就将加密串在方法里写好:{   $str1=rand(100000, 999999);  $verif = md5($str1);  session("phoneverify",$verif);   }。


public function phone_add(){

$phone_from = M("phone_from ");  //短信记录表


//得到发送短信的号码 

$phone = $_POST["phone"]; // 是get,自行修改;

if(!preg_match('/^[1]+[3,4,5,8,7]+\d{9}/',$phone)){
$this->error('号码错误');
exit;
}

//发送短信验证码

$stra = rand(100000, 999999);

$pverif = md5($stra);

session("phone_verify_phone",$pverif);

//验证加密串是否正确

if($_POST['phone_ver']==$_SESSION['phoneverify']){   //   phone_ver为页面传过来的加密串

//规则:短信五分钟内不能重发发送。

$date_phone['phone'] = $_POST["phone"]; //号码
$find_phone = $phone_from ->where($date_phone)->limit(1)->order("id desc")->select();

//判断上一次发送短信时间
$ttime = time()-$find_phone[0]['time'];

if($find_phone[0]['time']!='' && $ttime<300){

$this->error('五分钟内不能重复发送。');

}

//一天时间内不能大于5条
$date_date['phone'] = $_POST["phone"]; //号码
$date_date['time'] = (string) array('between',array(strtotime(date('Y-m-d 00:00:00')),strtotime(date('Y-m-d 23:59:59')))); //时间 
$smeid = $phone_from ->where($date_date)->count();

if($smeid>=5){

$this->error('短信一天不能超过五条');

}

$id = '接口用户名';
$password = '密码';
$to = $phone;  //号码
$Content = '您的验证码:'.$stra.',24分钟内有效,(请勿告知任何他人)。'; //短信内容
$content = iconv("UTF-8","GB2312",$Content);
$url="接口路径";
$data = "uid=%s&password=%s&to=%s&content=%s&time=";  //参数1

$rdata = sprintf($data, $id, $password, $to, $content);  //参数2

$ch = curl_init();
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$rdata);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result = curl_exec($ch);
curl_close($ch); //关闭连接

//请自行处理返回信息

if($result=='0'){

$date_short['time'] = time();
$date_short['phone'] = $telephone;
$date_short['content'] = $stra;
$phone->add($date_short);
  //将发送的短信写入记录表

$this->success("发送短信成功。",'跳转链接');

}else{

$this->error("发送短信失败。",'跳转链接'); //或短信条数不足;

}

}else{

$this->error('请不要重复发送');
exit;
}


}







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章