Home>Article>Backend Development> 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:
Back-end PHP processing code:
Root = APP_PATH."/data/msg_logs/";//自己定义的文件存放位置 } //获取短信验证码操作(Ajax方法为好) Public function get_authentication_code(){ if ($_POST['uv_r'] && $_POST['tel']) { $ip=$_SERVER["REMOTE_ADDR"];//ip $tel = $_POST['tel'];//电话 $uv_r = $_POST['uv_r'];//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!