<pre>
<?php
/**
* get_client_connects() Function
* Created by: Joshua H. (TRUSTAbyss)
*
* Usage: get_client_connects(PORT) will output an integer value
* based on the number of connections to the server. This function
* accepts an optional IP argument that can be used to show how
* many connections a single client has made.
*
* Note: The number of connections is based on the server's timeout;
* if you're server has a timeout of 15, a client connection will remain
* active until 15 seconds of inactivity.
*
* This function requires access to the exec() function and netstat
* command in order for it to work.
*/
function get_client_connects($port, $ip = '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
{
$connects = 0; // Initialize the $connects variable.
// Run the netstat command and output the contents to an array.
exec("netstat -n",
$array);
// Create the pattern that will be used to match the number of clients connected.
$pattern = "([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}[.:]{$port}[ ]+{$ip}[.:][0-9]+)[ ]+ESTABLISHED";
foreach ($array as $string) // Loop through the array.
{
{
$connects++; // Increase the number of connects by one.
}
}
// Output the number of connections made.
return $connects;
}
// Lets display the number of connections from the current user on port 80.
echo get_client_connects
(80,
$_SERVER['REMOTE_ADDR']).
' Connection(s) From '.
$_SERVER['REMOTE_ADDR'].
"\n";
// Lets display all connections on port 80!
echo get_client_connects
(80).
' Connection(s) From Total';
?>
</pre>