短信接口在业务中是必然会有的,那么怎么保证接口被刷呢?
我简单总结一下我的想法
/*** 手机登录验证码* @author marain* @time 2017-06-27* */public function get_code(){$phone = $_REQUEST['tel'];$token_key = $_REQUEST['token_key'];$key='marain';$now=date('Y-m-d');$signkey=md5($key.$now.$phone.'marain'); //根据手机号和时间产生 tokenif ($token_key !== $signkey){$result = array();$result['code'] =400;$result['msg'] ='4001:令牌错误';$result['info'] = '';echo json_encode($result);exit();}if ($phone==''){$result = array();$result['code'] =400;$result['msg'] ='4002:电话不能为空';$result['info'] = '';echo json_encode($result);exit();}if (!preg_match("/^1[34578]d{9}$/", $phone)){$result = array();$result['code'] =400;$result['msg'] ='4003:电话格式不正确';$result['info'] = '';echo json_encode($result);exit();}$ip=get_client_ip();$where_ip['create_ip']=$ip;//ip控制$sms_data = M('App_sms')->where($where_ip)->select();$today_date = date('Y-m-d');$total_onoip_count=0;foreach ($sms_data as $k1=>$v1){if(substr($v1['create_time'],0,10) == $today_date){$total_onoip_count++;}}if (count($sms_data) > 500){//ip 大于500报警}if ($total_onoip_count> 100){//单日ip 大于100报警}$where_who['tel_number']=$phone;$sms_data = M('App_sms')->where($where_who)->order("id desc")->select();//这个手机号没有注册if(empty($sms_data)){$code = $this->_create_code();$this->send_sms($phone, $code, $ip);$result = array();$result['code'] =200;$result['msg'] ='获取成功';$result['info'] = $code;echo json_encode($result);exit();}$total_send_count = 0;foreach($sms_data as $key1=>$row1){if(empty($key1)){$last_send_time = $row1['create_time'];}if(substr($row1['create_time'],0,10) == $today_date){$total_send_count++;$code = $row1['code'];}}if(empty($code)) $code = $this->_create_code();if((strtotime($last_send_time) + 60) > time()){$result['code'] =400;$result['msg'] ='4004:获取失败,请不要频繁获取';$result['info'] = '';}else{if($total_send_count < 30){$this->send_sms($phone, $code, $ip);$result['code'] =200;$result['msg'] ='获取成功';$result['info'] = $code;}else{$result['code'] =400;$result['msg'] ='4005:获取失败,每人每天只能获取三十次验证码';$result['info'] = '';}}echo json_encode($result);exit();}/*** 产生验证码*/public function _create_code($length=4,$type="number"){$array = array('number' => '0123456789','string' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','mixed' => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',);$string = $array[$type];$count = strlen($string)-1;$rand = '';for ($i = 0; $i < $length; $i++) {$rand .= $string[mt_rand(0, $count)];}return $rand;}
private function send_sms($mobile, $code, $ip){if(empty($mobile) || empty($code) || empty($ip)) return false;$content = '您的注册验证码是'.$code;$url='发送短信接口'//$send_result = file_get_contents($url);$data = array();$data['tel_number'] = $mobile;$data['content'] = $content;$data['code'] = $code;$data['create_time'] = date('Y-m-d H:i:s');$data['create_ip'] = get_client_ip();$data['send_result'] = $send_result;$insertid = M('App_sms')->add($data);if($insertid) return true;return false;}
本文发布于:2024-02-03 06:56:15,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170691457549379.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |