title: PHP: How to know if a user is currently downloading a file?
body: <p>I'm developing a quick rapidshare-like site where the user can download files. First, I created a quick test setting headers and using <code>readfile()</code> but then I found <a href="http://www.php.net/manual/en/function.readfile.php#83653" rel="nofollow">in the comments section</a> there's a way to limit the speed of the download, which is great, here's the code:</p>
  
  <pre><code>$local_file = 'file.zip';
  $download_file = 'name.zip';
  
  // set the download rate limit (=&gt; 20,5 kb/s)
  $download_rate = 20.5;
  if(file_exists($local_file) &amp;&amp; is_file($local_file))
  {
      header('Cache-control: private');
      header('Content-Type: application/octet-stream');
      header('Content-Length: '.filesize($local_file));
      header('Content-Disposition: filename='.$download_file);
  
      flush();
      $file = fopen($local_file, "r");
      while(!feof($file))
      {
          // send the current file part to the browser
          print fread($file, round($download_rate * 1024));
          // flush the content to the browser
          flush();
          // sleep one second
          sleep(1);
      }
      fclose($file);}
  else {
      die('Error: The file '.$local_file.' does not exist!');
  }
  </code></pre>
  
  <p>But now my question is, how to limit the number of downloads at the same time? How can I check there's still a connection with some user's IP?</p>
  
  <p>Thanks.</p>