25
2009
How many users are connected on http apache server
<?
if(isset($_GET['showsource'])) { highlight_file($_SERVER['SCRIPT_FILENAME']); die; }
function getIP() {
$ip=”";
if (getenv(“HTTP_CLIENT_IP”)) $ip = getenv(“HTTP_CLIENT_IP”);
else if(getenv(“HTTP_X_FORWARDED_FOR”)) $ip = getenv(“HTTP_X_FORWARDED_FOR”);
else if(getenv(“REMOTE_ADDR”)) $ip = getenv(“REMOTE_ADDR”);
else $ip = “”;
return $ip;
}
function howManyIps() {
$filename = “./howmanyip.log”;
$seconds = 300;
$yourIP = getIP();
if (file_exists($filename.”.lock”)) $readonly = true; else $readonly=false;
$count = 0;
//lock the file
if (!$readonly) $fpLock = fopen($filename.”.lock”, “w”);
//read data ips
$fp = @fopen($filename, “r”);
$arIPS=explode (“\n”, @fread($fp,filesize($filename)) );
@fclose($fp);
//if file is locked get out
if ($readonly) return count($arIPS);
$s = “”;
$already=false;
//update data and search user ip
for ($i=0;$i<count($arIPS);$i++) {
$arData= explode (” “, $arIPS[$i]);
//update your user timer
if ($yourIP==$arData[0]) {
$already=true;
$arData[1]=time();
}
// check if user is old
if ( time()- (integer)$arData[1] < $seconds ){
$s.=$arData[0].” “.$arData[1].”\n”;
$count++;
}
}
if (!$already) {
//your user is new, add it to the list
$s.=$yourIP.” “.time().”\n”;
$count++;
}
//save the list
$fp = fopen($filename, “w”);
fwrite($fp,$s);
fclose($fp);
//remove thr lock
fclose($fpLock);
unlink($filename.”.lock”);
return $count;
}
echo howManyIps().” connected.”;
?>
Vir: http://www.barattalo.it/category/php/page/3/
Leave a comment
Kategorije
- Ostalo (161)
- Računalništvo (266)
- Centos (34)
- CSS (2)
- FreeBSD (37)
- jQuery (1)
- MySQL (8)
- Na spletu dogaja … (124)
- Netbeans (4)
- PHP (20)
- Symbian (3)
- Ubuntu (32)
- Windows (5)
- Zend Framework (6)
- Zend Studio (1)
Blogroll
Arhiv
- February 2011 (5)
- December 2010 (4)
- November 2010 (2)
- October 2010 (5)
- September 2010 (6)
- August 2010 (5)
- July 2010 (10)
- June 2010 (9)
- May 2010 (10)
- April 2010 (8)
- March 2010 (12)
- February 2010 (19)
- January 2010 (20)
- December 2009 (10)
- November 2009 (1)
- October 2009 (3)
- September 2009 (2)
- August 2009 (9)
- June 2009 (11)
- May 2009 (5)
- April 2009 (6)
- March 2009 (9)
- February 2009 (15)
- January 2009 (10)
- December 2008 (17)
- November 2008 (20)
- October 2008 (16)
- September 2008 (48)
- August 2008 (56)
- July 2008 (35)
- June 2008 (35)
An article by




