繁体   English   中英

限制每周每位用户在php中的搜索次数

[英]Limit number of searches per user per week in php

我已经开发了一个搜索页面,只有登录的用户才能访问。 但是我想将每个用户的搜索限制在一周内仅20次。 一个用户的搜索次数不能超过20次。 请帮助我如何实现这一目标。

在用户表中实现两个列:
列1:search_counts-默认0
第2列:search_timestamp-默认0
当用户启动搜索时:
检查timmstap,如果它早于一个星期,则将其设置为当前时间(现在),并将计数器设置为1,然后允许搜索,如果不是,则检查计数器是否等于最大允许的搜索,然后中止它,否则增加计数器并允许搜索。 假设您将用户表中的couner和timstamp值传递给它,这是一个未经测试的函数:

function search_check($counter,$ts,$max=20,$period =7) {
 $p = $period * 24 * 60 * 60;  
 $now = time();
  if(($now - $ts) >= $p ){
  //set the time stamp to now in DB
 //set the counter to 1 in DB
 return true;
  }else {
       if($counter < $max){ // 
      //increment the counter by 1 in DB
       return true;
       }else{
        return false;
      }

     }
   }

用法:

//retrieve counter and timestamp values from the DB users table
$can_search = search_check($counter,$ts);
 if($can_search){
 //search and return search result
 }else{
 echo "Sorry, maximum weekly searches reached"
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM