Page 1 of 1

number of visitors online

Posted: Thu Jan 17, 2008 10:56 am
by Gregor
Hi,

Found some php-code  to use for showing the number of online users http://www.roscripts.com/How_many_visitors_online_PHP_script-108.html
From what I read, it's free to use:

Create a file called "visitors.txt" and upload it to the root of your site. Also upload the following php-code:

Code: Select all

<?php
$dataFile = "visitors.txt";

$sessionTime = 30; // this is the time in **minutes** to consider someone online before removing them from our file

//Please do not edit bellow this line

error_reporting(E_ERROR | E_PARSE);

if(!file_exists($dataFile)) {
	$fp = fopen($dataFile, "w+");
	fclose($fp);
}

$ip = $_SERVER['REMOTE_ADDR'];
$users = array();
$onusers = array();

//getting
$fp = fopen($dataFile, "r");
flock($fp, LOCK_SH);
while(!feof($fp)) {
	$users[] = rtrim(fgets($fp, 32));
}
flock($fp, LOCK_UN);
fclose($fp);


//cleaning
$x = 0;
$alreadyIn = FALSE;
foreach($users as $key => $data) {
	list( , $lastvisit) = explode("|", $data);
	if(time() - $lastvisit >= $sessionTime * 60) {
		$users[$x] = "";
	} else {
		if(strpos($data, $ip) !== FALSE) {
			$alreadyIn = TRUE;
			$users[$x] = "$ip|" . time(); //updating
		}
	}
	$x++;
}

if($alreadyIn == FALSE) {
	$users[] = "$ip|" . time();
}

//writing
$fp = fopen($dataFile, "w+");
flock($fp, LOCK_EX);
$i = 0;
foreach($users as $single) {
	if($single != "") {
		fwrite($fp, $single . "\r\n");
		$i++;
	}
}
flock($fp, LOCK_UN);
fclose($fp);

//:auto; background-color:#fff
if($uo_keepquiet != TRUE) {
	if($i = 1) {
	echo '<div style="padding:5px; margin:auto"><b>' . $i . ' bezoeker online</b></div>';	
	} else {
		echo '<div style="padding:5px; margin:auto"><b>' . $i . ' bezoekers online</b></div>';
	}
}
?>
Create a UDT, in my case visitors, that has the following code:

Code: Select all

include_once "/home/gregor/domains/uisge-beatha.eu/public_html/visitors.php";
Place the tag {vistors} somewhere in your template. I'm about to place it in the footer. To see an example www.uisge-beatha.eu At the moment it's placed on the right hand site on the home page.

Good luck,
Gregor

Re: number of visitors online

Posted: Thu Jan 17, 2008 2:06 pm
by Gregor
Based on one of the other topics, an easier method is to put the php-code direct into the udt, so visitor.php does not to be placed on the server. The code for the UDT is without the
.

Code: Select all

$dataFile = "visitors.txt";

$sessionTime = 30; // this is the time in **minutes** to consider someone online before removing them from our file

//Please do not edit bellow this line

error_reporting(E_ERROR | E_PARSE);

if(!file_exists($dataFile)) {
	$fp = fopen($dataFile, "w+");
	fclose($fp);
}

$ip = $_SERVER['REMOTE_ADDR'];
$users = array();
$onusers = array();

//getting
$fp = fopen($dataFile, "r");
flock($fp, LOCK_SH);
while(!feof($fp)) {
	$users[] = rtrim(fgets($fp, 32));
}
flock($fp, LOCK_UN);
fclose($fp);


//cleaning
$x = 0;
$alreadyIn = FALSE;
foreach($users as $key => $data) {
	list( , $lastvisit) = explode("|", $data);
	if(time() - $lastvisit >= $sessionTime * 60) {
		$users[$x] = "";
	} else {
		if(strpos($data, $ip) !== FALSE) {
			$alreadyIn = TRUE;
			$users[$x] = "$ip|" . time(); //updating
		}
	}
	$x++;
}

if($alreadyIn == FALSE) {
	$users[] = "$ip|" . time();
}

//writing
$fp = fopen($dataFile, "w+");
flock($fp, LOCK_EX);
$i = 0;
foreach($users as $single) {
	if($single != "") {
		fwrite($fp, $single . "\r\n");
		$i++;
	}
}
flock($fp, LOCK_UN);
fclose($fp);

//:auto; background-color:#fff
if($uo_keepquiet != TRUE) {
	if($i = 1) {
	echo '<div style="padding:5px; margin:auto"><b>' . $i . ' bezoeker online</b></div>';	
	} else {
		echo '<div style="padding:5px; margin:auto"><b>' . $i . ' bezoekers online</b></div>';
	}
}

Re: number of visitors online

Posted: Wed Jan 23, 2008 11:33 am
by Courty
Couldn't get this to work at first on CMSMS 1.2.3 but all working now.

Here's a version that works on 1.2.3, that outputs in English and doesn't force any formatting on the output.

I put it in the Footer and it works fine.

I added it as a "User Defined Tag" called visitor and called it from the footer by adding {visitor} to the end of the copyright line.

Courty  8)

Code: Select all

$dataFile = "visitors.txt";

$sessionTime = 30; //this is the time in **minutes** to consider someone online before removing them from our file

//Please do not edit bellow this line

error_reporting(E_ERROR | E_PARSE);

if(!file_exists($dataFile)) {
	$fp = fopen($dataFile, "w+");
	fclose($fp);
}

$ip = $_SERVER['REMOTE_ADDR'];
$users = array();
$onusers = array();

//getting
$fp = fopen($dataFile, "r");
flock($fp, LOCK_SH);
while(!feof($fp)) {
	$users[] = rtrim(fgets($fp, 32));
}
flock($fp, LOCK_UN);
fclose($fp);


//cleaning
$x = 0;
$alreadyIn = FALSE;
foreach($users as $key => $data) {
	list( , $lastvisit) = explode("|", $data);
	if(time() - $lastvisit >= $sessionTime * 60) {
		$users[$x] = "";
	} else {
		if(strpos($data, $ip) !== FALSE) {
			$alreadyIn = TRUE;
			$users[$x] = "$ip|" . time(); //updating
		}
	}
	$x++;
}

if($alreadyIn == FALSE) {
	$users[] = "$ip|" . time();
}

//writing
$fp = fopen($dataFile, "w+");
flock($fp, LOCK_EX);
$i = 0;
foreach($users as $single) {
	if($single != "") {
		fwrite($fp, $single . "\r\n");
		$i++;
	}
}
flock($fp, LOCK_UN);
fclose($fp);

if($uo_keepquiet != TRUE) {
if($i != 1){
echo  $i . ' visitors online';
}else{
echo $i . ' visitor online';
}
}