PHP Function to get Client Connections.

 
Post new topic   Reply to topic    Aprelium Forum Index -> Off Topic Discussions
View previous topic :: View next topic  
Author Message
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Tue Dec 05, 2006 4:25 am    Post subject: PHP Function to get Client Connections. Reply with quote

Hello everyone,

I have another great function. This new function allows you to get the exact
number of connections a user has made on your server. It works on Windows
and all other OSs that support the netstat command.

Update: December 13, 2006

The function now has the ability to look up all connections to the
server. If the IP argument is empty, the function creates a simple
Regular Expression to look up all connections.

Code:
<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.
    {
        if (preg_match("/$pattern/i", $string))
        {
            $connects++; // Increase the number of connects by one.
        }
    }
   
    // Output the number of connections made.
    return $connects;
}

// Syntax: get_client_connects(ip, port)
// Note: port is optional

$ip = $_SERVER['REMOTE_ADDR'];

// Lets display connections from current user
echo get_client_connects($ip) . " Connection(s) From " . $ip . "\n";

// Lets display all connections on port 80!
echo get_client_connects() . " Connection(s) From Total";
?>
</pre>


Note: I will compile a full list of all of my functions and release them in
the near future. For now, here's another great function.

Sincerely, Josh (TRUSTAbyss)


Last edited by TRUSTAbyss on Mon Aug 04, 2008 9:04 pm; edited 6 times in total
Back to top View user's profile Send private message Visit poster's website
Ralph
-


Joined: 17 Sep 2006
Posts: 87

PostPosted: Tue Dec 05, 2006 9:11 am    Post subject: Reply with quote

Very cool TRUSTAbyss,

No long, spread out, log file to look through, it gets you right to the exact IP connection and how many times. How awesome and it works great! I will be keeping that in my htdocs for sure...I like it.
_________________
My Abyss Websites!
www.qualitynetdesign.com
Back to top View user's profile Send private message Visit poster's website
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Tue Dec 05, 2006 10:07 am    Post subject: Reply with quote

I'm glad you like it. I'm sure I will come up with more in the future.
Back to top View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> Off Topic Discussions All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group