在 php 内限制每个用户每周的搜索次数
Limit number of searches per user per week in php
我开发了一个搜索页面,只有登录用户才能访问。但我想将每个用户的搜索限制为一周内只搜索 20 次。一个用户将无法搜索超过 20 次。请帮助我如何实现这一目标。
在用户中实现两个列 table:
第 1 列:search_counts - 默认 0
第 2 列:search_timestamp - 默认 0
当用户发起搜索时:
检查 timmstap,如果它超过一周,将其设置为当前时间(现在)并将计数器设置为 1 并允许搜索,如果不是,请检查计数器是否等于允许的最大搜索然后中止它,否则增加计数器和允许搜索。
这是一个未经测试的函数,假设您将用户 table 的 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"
}
我开发了一个搜索页面,只有登录用户才能访问。但我想将每个用户的搜索限制为一周内只搜索 20 次。一个用户将无法搜索超过 20 次。请帮助我如何实现这一目标。
在用户中实现两个列 table:
第 1 列:search_counts - 默认 0
第 2 列:search_timestamp - 默认 0
当用户发起搜索时:
检查 timmstap,如果它超过一周,将其设置为当前时间(现在)并将计数器设置为 1 并允许搜索,如果不是,请检查计数器是否等于允许的最大搜索然后中止它,否则增加计数器和允许搜索。
这是一个未经测试的函数,假设您将用户 table 的 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"
}