tags:

views:

127

answers:

3

hi friends

I want to build a code (PHP) for detecting online user's ip

I have installed Discuz forum. it shows the ips itself.

what i want is something different.

I want to built a code to see the ip of all online users in one page.

how i can do that ?

I wrote a code. see:

function getRealIpAddr() 
{
  if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
  {
    $ip=$_SERVER['HTTP_CLIENT_IP'];
  }
  elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
  {
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  }
  else
 {
   $ip=$_SERVER['REMOTE_ADDR'];
 }

 return $ip;
}

usage:

$ipp=getRealIpAddr();
echo "real ip=".$ipp;

BUT I want to see the ip of whole online users in one page like a log.

I tried to be clear this time.

A: 

If you don't feel like just checking your server logs as Ishmael suggests, you could do your own logging to a db perhaps. You can't just have PHP know everyone who's currently online. "Currently online" isn't a real concept anyway (unless you're talking about the minuscule time frame during which the page is being executed.) It usually means "in the last X minutes". Things like forum software just log last access time, and show users the count of people whose last access was in the last X minutes. So you could do the same thing but record IP as well.

Tesserex
thanks for answer, i want to have a page that lists the IP of online users, very simple.
safaali
A: 

This is what you're looking for...

Shadi Almosri
A: 

I cannot give you an answer to how to implement this with the forum software you are using. I've checked the site and they have forum for mods and plugins, so you might want to ask there if this has to be a plugin (unless someone here knows it of course).

To find out the IPs of all users currently logged in to your site, you could do something along the lines of this (we define currently logged in as 'has a session'):

// put in your bootstrap after you defined getRealIpAddr()
if(session_id() === '') {
    session_start();
}
$_SESSION['ip'] = getRealIpAddr();

Then in your lib add a SessionReader class, e.g.

class SessionReader
{
    // serializes session data without destroying your own session 
    public function decode($filecontent){
        // see http://de.php.net/manual/de/function.session-decode.php#69111
    }

    // just reads in the contents of a session file
    public function readSessionData($file)
    {
        return file_get_contents(realpath("$session_save_path/$file"));
    }

    // returns all filenames in save path starting with 'sess'
    public function getSessionFiles()
    {
        $path = realpath(session_save_path());
        return glob($path . '/sess*');
    }

    // uses the above methods to build an array of all decoded sessions
    public function getEveryonesSessionData()
    {
        $contents = array_filter($this->getSessionFiles(), 
                                 array($this, "readSessionData"));

        return array_filter($contents, array($this, "decode")); 
    }
}

To use it, do

$sessionReader = new SessionReader;
foreach( $sessionReader->getEveryonesSessionData() as $session) {
    echo $session['IP'];
}

Disclaimer: this is just a proof of concept aka ugly hack. I don't expect this to work without adjustment. so you shouldn't either. But you should be able to get it done from here.

The SessionReader should be able to read in all session files in the session_save_path specified in the PHP.ini or elsewhere in your app. The assumption is, session files start with sess and are stored in the file system. When calling getEveryonesSessionData(), the class will find, read, decode and return all session files in an array, so you can access them one by one.

To get this running with your forum software, you have to find out how they define currently logged in users and if and how they use sessions.

Gordon