[英]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.