title: How to limit the number of user requests made within a minute
body: <p>A user will request a file by number via a URL like <code>script.php?userid=222</code>. This example would show the record of file #222.</p>
  
  <p>Now I want to limit the number of files per (remote IP) user to a maximum of 5 different records in a minute. However, the user should be able to access the same id record any number of time.</p>
  
  <p>So the user could access file #222 any number of times, but if the (remote IP) user accesses more than 5 other different records in a minute, then it should show an error.</p>
  
  <p>For example, suppose within a minute the following requests are made:</p>
  
  <pre><code>script.php?userid=222
  script.php?userid=523
  script.php?userid=665
  script.php?userid=852
  script.php?userid=132
  script.php?userid=002
  </code></pre>
  
  <p>then at the last request it should show the error message.</p>
  
  <p>Here is the basic code:</p>
  
  <pre><code>$id = $_GET['userid'];
  if (!isset($_GET['userid']) || empty($_GET['userid'])) {
      echo "Please enter the userid";
      die();
  }
  
  if (file_exists($userid.".txt") &amp;&amp;
          (filemtime($userid.".txt") &gt; (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");
  }
  </code></pre>