Home  >  Article  >  Backend Development  >  Implementation of SMS verification code sending limit analysis based on PHP

Implementation of SMS verification code sending limit analysis based on PHP

coldplay.xixi
coldplay.xixiforward
2020-07-13 17:30:582763browse

Implementation of SMS verification code sending limit analysis based on PHP

Restrict the mobile phone number, IP, and browser (using a unique identifier) ​​for users to obtain SMS verification codes. The method introduced in this article is that users can only obtain verification codes 10 times a day through the same browser or the same IP address, or can only obtain SMS verification codes 3 times with the same mobile phone number. The three restrictions are in an "or" relationship. If one exceeds the limit, it will Send verification code. The method is to record and write the user's mobile phone number, ip, ur_r on the server side to a file, and then determine the number of times the user requests to send a verification code by reading the file record to limit the number. . The method is as follows:

Get the SMS verification code page:

<!DOCTYPE html>
<html>
<head></head>
<body>
<!-- 隐藏表单uv_r标识,用于对获取验证码的浏览器进行限制,唯一标识存储于浏览器cookie中。在用户进行获取短信验证码操作时将标识传入后台代码(可以通过js传入后台,此处未提供js代码) -->
<input type="hidden" name="uv_r" value="" id="uv_r">
</body>
<script type=”text/javascript”>
/*
使用js获取cookie中ur_r唯一标识,如果不存在,生成唯一标识,js写入cookie,并将唯一标识赋给隐藏表单。
*/
 //唯一标识存入cookie
    var _uuid = getUUID();
    if(getCookie("_UUID_UV")!=null && getCookie("_UUID_UV")!=undefined)
    {
      _uuid = getCookie("_UUID_UV");
    }else{
      setCookie("_UUID_UV",_uuid);
    }
    document.getElementById("uv_r").value = _uuid;//赋给hidden表单
    //生成唯一标识
    function getUUID()
    {
      var uuid = new Date().getTime();
      var randomNum =parseInt(Math.random()*1000);
      return uuid+randomNum.toString();
    }
    //写cookie
    function setCookie(name,value)
    {
      var Days = 365;//这里设置cookie存在时间为一年
      var exp = new Date();
      exp.setTime(exp.getTime() + Days*24*60*60*1000);
      document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
    }
    //获取cookie
    function getCookie(name)
    {
      var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
      if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
      else
        return null;
    }
</script>
</html>

Back-end PHP processing code:

<?php
Class regMod{
//定义全局变量,用于设置记录文件的路径
Protected $Root = null;
Public function __construct(){
$this -> Root = APP_PATH."/data/msg_logs/";//自己定义的文件存放位置
}
//获取短信验证码操作(Ajax方法为好)
Public function get_authentication_code(){
if ($_POST[&#39;uv_r&#39;] && $_POST[&#39;tel&#39;]) {
$ip=$_SERVER["REMOTE_ADDR"];//ip
  $tel = $_POST[&#39;tel&#39;];//电话
  $uv_r = $_POST[&#39;uv_r&#39;];//ur_r标识
  if(empty($uv_r)){
    $uv_r = 0;
  }
}

      //判断数据是否超过了限制
$uvr_num = $this->checkUvr($uv_r);
$tel_num = $this->checkTel($tel);
$ip_num = $this->checkIp($ip);

if ($uvr_num < 10 && $tel_num < 4 && $ip_num < 10) {
Echo "发送验证码";//符合发送条件,发送验证码的操作
} else {
Echo “不发送验证码”;
//当不发送验证码时,将数据存入文件,用于方便查询
$data = $tel . "|" . $ip . "|" . $uv_r . "|";
  if ($uv_r>0 && $uvr_num >= 10) {
    $data = $data . "A@";
  }
  if ($tel_num >= 4) {
    $data = $data . "B@";
  }
  if ($ip_num >= 10) {
    $data = $data . "C@";
  }
  $this->wirteFile("", $data);
  $this->ajax_return(0, "您今日获取短信验证码的次数过多!");//给用户返回信息,ajax_return()为自写方法(未提供)
  }
}
//以下方法为私有方法
//检测ur_r在文件中出现的次数
Private function checkUvr($data){
  $fileName = "Uv_".date("Ymd",time()).".dat";
  $filePath = ($this -> Root).$fileName;//组装要写入的文件的路径
  $c_sum = 0;
  if(file_exists($filePath)){//文件存在获取次数并将此次请求的数据写入
    $arr=file_get_contents($filePath);
    $row=explode("|",$arr);
    $countArr=array_count_values($row);
    $c_sum = $countArr[$data];
    if($c_sum<10)
    {
      $this -> wirteFile($filePath,$data."|");
    }
    return $c_sum;
  }else{//文件不存在创建文件并写入本次数据,返回次数0
    $this -> wirteFile($filePath,$data."|");
    return $c_sum;
  }
}
//检测Tel在文件中出现的次数
Private function checkTel($data){
  $fileName = "Tel_".date("Ymd",time()).".dat";
  $filePath = ($this -> Root).$fileName;
  $c_sum = 0;
  if(file_exists($filePath)){
    $arr=file_get_contents($filePath);
    $row=explode("|",$arr);
    $countArr=array_count_values($row);
    $c_sum = $countArr[$data];
    if($c_sum<4)
    {
      $this -> wirteFile($filePath,$data."|");
    }
    return $c_sum;
  }else{
    $this -> wirteFile($filePath,$data."|");
    return $c_sum;
  }
}
//检测IP在文件中存在的次数
Private function checkIp($data){
  $fileName = "Ip_".date("Ymd",time()).".dat";
  $filePath = ($this -> Root).$fileName;
  $c_sum = 0;
  if(file_exists($filePath)){
    $arr=file_get_contents($filePath);
    $row=explode("|",$arr);
    $countArr=array_count_values($row);
    $c_sum = $countArr[$data];
    if($c_sum<10)
    {
      $this -> wirteFile($filePath,$data."|");
    }
    return $c_sum;
  }else{
    $this -> wirteFile($filePath,$data."|");
    return $c_sum;
  }
}
/**
* 将数据写入本地文件
* @param $filePath 要写入文件的路径
* @param $data 写入的数据
*/
Private function wirteFile($filePath,$data){
try {
    if(!is_dir($this->Root)){//判断文件所在目录是否存在,不存在就创建
      mkdir($this->Root, 0777, true);
    }
    if($filePath==""){//此处是不发送验证码时,记录日志创建的文件
      $filePath = ($this -> Root)."N".date("Ymd",time()).".dat";
    }
//写入文件操作
    $fp=fopen($filePath,"a+");//得到指针
    fwrite($fp,$data);//写
    fclose($fp);//关闭
  } catch (Exception $e) { print $e->getMessage();  }
}

}
?>

The above is the entire content of this article, I hope it will be helpful to everyone's study .

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of Implementation of SMS verification code sending limit analysis based on PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete