zoukankan      html  css  js  c++  java
  • PHP实现对短信验证码发送次数的限制(防机刷验证码)

    PHP实现对短信验证码发送限制(防止机刷验证码)

      对用户获取短信验证码的手机号、ip、和浏览器(使用唯一标识)进行限制。本文介绍的方法是对用户每天只能通过同一浏览器或同一ip地址获取验证码10次或者同一手机号只能获取3次短信验证码,三种限制为“或”关系,一条超限就不发验证码。方法是通过在服务器端将用户的手机号、ipur_r记录并写入文件,再通过读取文件记录判断用户请求发送验证码的次数来做限制。方法如下:

    获取短信验证码页面:

     1 <!DOCTYPE html>
     2 <html>
     3 <head></head>
     4 <body>
     5 <!-- 隐藏表单uv_r标识,用于对获取验证码的浏览器进行限制,唯一标识存储于浏览器cookie中。在用户进行获取短信验证码操作时将标识传入后台代码(可以通过js传入后台,此处未提供js代码) -->
     6 <input type="hidden" name="uv_r" value="" id="uv_r">
     7 </body>
     8 <script type=”text/javascript”>
     9 /*
    10 使用js获取cookie中ur_r唯一标识,如果不存在,生成唯一标识,js写入cookie,并将唯一标识赋给隐藏表单。
    11 */
    12  //唯一标识存入cookie
    13         var _uuid = getUUID();
    14         if(getCookie("_UUID_UV")!=null && getCookie("_UUID_UV")!=undefined)
    15         {
    16             _uuid = getCookie("_UUID_UV");
    17         }else{
    18             setCookie("_UUID_UV",_uuid);
    19         }
    20         document.getElementById("uv_r").value = _uuid;//赋给hidden表单
    21         //生成唯一标识
    22         function getUUID()
    23         {
    24             var uuid = new Date().getTime();
    25             var randomNum =parseInt(Math.random()*1000);
    26             return uuid+randomNum.toString();
    27         }
    28         //写cookie
    29         function setCookie(name,value)
    30         {
    31             var Days = 365;//这里设置cookie存在时间为一年
    32             var exp = new Date();
    33             exp.setTime(exp.getTime() + Days*24*60*60*1000);
    34             document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
    35         }
    36         //获取cookie
    37         function getCookie(name)
    38         {
    39             var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    40             if(arr=document.cookie.match(reg))
    41                 return unescape(arr[2]);
    42             else
    43                 return null;
    44         }
    45 </script>
    46 </html>

    后端PHP处理代码:

      1 <?php 
      2 Class regMod{
      3 //定义全局变量,用于设置记录文件的路径
      4 Protected $Root = null;
      5 Public function __construct(){
      6 $this -> Root = APP_PATH."/data/msg_logs/";//自己定义的文件存放位置
      7 }
      8 //获取短信验证码操作(Ajax方法为好)
      9 Public function get_authentication_code(){
     10 if ($_POST['uv_r'] && $_POST['tel']) {
     11 $ip=$_SERVER["REMOTE_ADDR"];//ip
     12     $tel = $_POST['tel'];//电话
     13     $uv_r = $_POST['uv_r'];//ur_r标识
     14     if(empty($uv_r)){
     15         $uv_r = 0;
     16     }
     17 }
     18 
     19             //判断数据是否超过了限制
     20 $uvr_num = $this->checkUvr($uv_r);
     21 $tel_num = $this->checkTel($tel);
     22 $ip_num = $this->checkIp($ip);
     23 
     24 if ($uvr_num < 10 && $tel_num < 4 && $ip_num < 10) {
     25 Echo "发送验证码";//符合发送条件,发送验证码的操作
     26 } else {
     27 Echo “不发送验证码”;
     28 //当不发送验证码时,将数据存入文件,用于方便查询
     29 $data = $tel . "|" . $ip . "|" . $uv_r . "|";
     30     if ($uv_r>0 && $uvr_num >= 10) {
     31         $data = $data . "A@";
     32     }
     33     if ($tel_num >= 4) {
     34         $data = $data . "B@";
     35     }
     36     if ($ip_num >= 10) {
     37         $data = $data . "C@";
     38     }
     39     $this->wirteFile("", $data);
     40     $this->ajax_return(0, "您今日获取短信验证码的次数过多!");//给用户返回信息,ajax_return()为自写方法(未提供)
     41     }
     42 }
     43 //以下方法为私有方法
     44 //检测ur_r在文件中出现的次数
     45 Private function checkUvr($data){
     46     $fileName = "Uv_".date("Ymd",time()).".dat";
     47     $filePath = ($this -> Root).$fileName;//组装要写入的文件的路径
     48     $c_sum = 0;
     49     if(file_exists($filePath)){//文件存在获取次数并将此次请求的数据写入
     50         $arr=file_get_contents($filePath);
     51         $row=explode("|",$arr);
     52         $countArr=array_count_values($row);
     53         $c_sum = $countArr[$data];
     54         if($c_sum<10)
     55         {
     56             $this -> wirteFile($filePath,$data."|");
     57         }
     58         return $c_sum;
     59     }else{//文件不存在创建文件并写入本次数据,返回次数0
     60         $this -> wirteFile($filePath,$data."|");
     61         return $c_sum;
     62     }
     63 }
     64 //检测Tel在文件中出现的次数
     65 Private function checkTel($data){
     66     $fileName = "Tel_".date("Ymd",time()).".dat";
     67     $filePath = ($this -> Root).$fileName;
     68     $c_sum = 0;
     69     if(file_exists($filePath)){
     70         $arr=file_get_contents($filePath);
     71         $row=explode("|",$arr);
     72         $countArr=array_count_values($row);
     73         $c_sum = $countArr[$data];
     74         if($c_sum<4)
     75         {
     76             $this -> wirteFile($filePath,$data."|");
     77         }
     78         return $c_sum;
     79     }else{
     80         $this -> wirteFile($filePath,$data."|");
     81         return $c_sum;
     82     }
     83 }
     84 //检测IP在文件中存在的次数
     85 Private function checkIp($data){
     86     $fileName = "Ip_".date("Ymd",time()).".dat";
     87     $filePath = ($this -> Root).$fileName;
     88     $c_sum = 0;
     89     if(file_exists($filePath)){
     90         $arr=file_get_contents($filePath);
     91         $row=explode("|",$arr);
     92         $countArr=array_count_values($row);
     93         $c_sum = $countArr[$data];
     94         if($c_sum<10)
     95         {
     96             $this -> wirteFile($filePath,$data."|");
     97         }
     98         return $c_sum;
     99     }else{
    100         $this -> wirteFile($filePath,$data."|");
    101         return $c_sum;
    102     }
    103 }
    104 /**
    105 * 将数据写入本地文件
    106 * @param $filePath 要写入文件的路径
    107 * @param $data 写入的数据
    108 */
    109 Private function wirteFile($filePath,$data){
    110 try {
    111         if(!is_dir($this->Root)){//判断文件所在目录是否存在,不存在就创建
    112             mkdir($this->Root, 0777, true);
    113         }
    114         if($filePath==""){//此处是不发送验证码时,记录日志创建的文件
    115             $filePath = ($this -> Root)."N".date("Ymd",time()).".dat";
    116         }
    117 //写入文件操作
    118         $fp=fopen($filePath,"a+");//得到指针
    119         fwrite($fp,$data);//
    120         fclose($fp);//关闭
    121     } catch (Exception $e) { print $e->getMessage();    }
    122 }
    123 
    124 }
    125 ?>
  • 相关阅读:
    Mac使用nginx+rtmp服务器
    iOS开发必不可少的76个工具
    AVPlayer缓存实现
    视频直播分析
    github部分有意思的库记录
    iOS四种多线程(swift和oc)
    Java的static关键字浅析
    JavaScript获取屏幕参数
    JavaScript中XMLHttpRequest实现跨域访问
    【CenchaTouch 学习笔记】Controller
  • 原文地址:https://www.cnblogs.com/blogforly/p/5841811.html
Copyright © 2011-2022 走看看