1. <pre>
  2. <?php
  3. /**
  4.  * get_client_connects() Function
  5.  * Created by: Joshua H. (TRUSTAbyss)
  6.  *
  7.  * Usage: get_client_connects(PORT) will output an integer value
  8.  * based on the number of connections to the server. This function
  9.  * accepts an optional IP argument that can be used to show how
  10.  * many connections a single client has made.
  11.  *
  12.  * Note: The number of connections is based on the server's timeout;
  13.  * if you're server has a timeout of 15, a client connection will remain
  14.  * active until 15 seconds of inactivity.
  15.  *
  16.  * This function requires access to the exec() function and netstat
  17.  * command in order for it to work.
  18.  */
  19.  
  20. function get_client_connects($port, $ip = '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
  21. {
  22. $connects = 0; // Initialize the $connects variable.
  23.  
  24. // Run the netstat command and output the contents to an array.
  25. exec("netstat -n", $array);
  26.  
  27. // Create the pattern that will be used to match the number of clients connected.
  28. $pattern = "([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}[.:]{$port}[ ]+{$ip}[.:][0-9]+)[ ]+ESTABLISHED";
  29.  
  30. foreach ($array as $string) // Loop through the array.
  31. {
  32. if (preg_match("/$pattern/i", $string))
  33. {
  34. $connects++; // Increase the number of connects by one.
  35. }
  36. }
  37.  
  38. // Output the number of connections made.
  39. return $connects;
  40. }
  41.  
  42. // Lets display the number of connections from the current user on port 80.
  43. echo get_client_connects(80, $_SERVER['REMOTE_ADDR']).' Connection(s) From '.$_SERVER['REMOTE_ADDR']."\n";
  44.  
  45. // Lets display all connections on port 80!
  46. echo get_client_connects(80).' Connection(s) From Total';
  47. ?>
  48. </pre>