The user will request the file by number via the URL, for example script.php?userid=222 . In this example, file entry # 222 will be shown.
Now I want to limit the number of files per user (remote IP) to 5 different records per minute. However, the user must be able to access the same id record for any amount of time.
Thus, the user can access file # 222 any number of times, but if the user (remote IP) accesses more than 5 other entries in a minute, then he should show an error.
For example, suppose the following requests are made within a minute:
script.php?userid=222 script.php?userid=523 script.php?userid=665 script.php?userid=852 script.php?userid=132 script.php?userid=002
then at the last request it should show an error message.
Here is the basic code:
$id = $_GET['userid']; if (!isset($_GET['userid']) || empty($_GET['userid'])) { echo "Please enter the userid"; die(); } if (file_exists($userid.".txt") && (filemtime($userid.".txt") > (time() - 3600 * $ttime ))) { $ffile = file_get_contents($userid.".txt");} else { $dcurl = curl_init(); $ffile = fopen($userid.".txt", "w+"); curl_setopt($dcurl, CURLOPT_URL,"http://remoteserver.com/data/$userid"); curl_setopt($dcurl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($dcurl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt($dcurl, CURLOPT_TIMEOUT, 50); curl_setopt($dcurl, CURLOPT_FILE, $ffile); $ffile = curl_exec($dcurl); if(curl_errno($dcurl)) // check for execution errors { echo 'Script error: ' . curl_error($dcurl); exit; } curl_close($dcurl); $ffile = file_get_contents($userid.".txt"); }
source share