Sj03rs
Sj03rs

Reputation: 937

Get list of connected IP addresses PHP

I'm developing an app which requires the IP address people who are on my website. So, I've seen that people use $_SERVER['REMOTE_ADDR']; Or

function GetIP()
{
    if (isset($_SERVER)) { if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) 
    { 
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; 
    } 
        elseif(isset($_SERVER["HTTP_CLIENT_IP"])) 
    { 
        $ip = $_SERVER["HTTP_CLIENT_IP"]; 
    } 
    else 
    {
        $ip = $_SERVER["REMOTE_ADDR"]; 
    } 
} 
    else 
    {
        if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) 
            { 
                $ip = getenv( 'HTTP_X_FORWARDED_FOR' ); 
            } 

        elseif ( getenv( 'HTTP_CLIENT_IP' ) ) 
            {
                $ip = getenv( 'HTTP_CLIENT_IP' ); 
            } 

        else 
            { 
                $ip = getenv( 'REMOTE_ADDR' );  
            } 
} 
return $ip; 
}

For more correct IP addresses. If the IP address isn't correct in my app, it shouldn't be a problem.
But I need a list of the IP addresses who are connected to the server. My question is, how is this possible?
How can I go get all the IP addresses connected to the server?
Because if I use the code, it only shows one address. I've tried using loops in my code but it unfortunately didn't work.
Any suggestions? Thoughts? I'd appreciate it!

Kind regards,

Sjors

Upvotes: 1

Views: 1351

Answers (2)

deceze
deceze

Reputation: 522101

There's only a very small number of active connections at any one time. The browser connects to your server, requests a website, gets the response, and then disconnects. It is only actually connected to your server momentarily. How long the user will leave the page open in their browser is an entirely different matter.

If you want something like this, you need to define what you mean by "connected." Probably something like "IP addresses which have requested a page within the last x minutes." You'll simply have to save the $_SERVER['REMOTE_ADDR'] of visitors in a database with a timestamp, and get the latest x IPs from that database.

Upvotes: 2

doraemon
doraemon

Reputation: 398

You can use sessions.

https://ellislab.com/codeigniter/user-guide/libraries/sessions.html

Save session data to a database table. And get all the IP addresses for active sessions.

You can do this on the core container, so in every request to your application the list is updated, and you can use the data on all containers of your application.

Upvotes: 0

Related Questions