Home Advanced Code Number of People on a Page

Number of People on a Page

E-mail Print PDF
Open up your notepad and save the current BLANK document as usersonline.txt (do not put anything in this document) - click edit->save as and place it in a folder where you can upload it later.

Close that document.

Open up notepad again and add the following script:

Code:
<?
$remote = $_SERVER["REMOTE_ADDR"];
$file = "usersonline.txt";
$timeoutseconds = 60; //how long the user stays on, before classed offline
$timestamp = time();
$timeout = ($timestamp-$timeoutseconds);
$fp = fopen("$file", "a+");
$write = $remote."||".$timestamp."\n";
fwrite($fp, $write);
fclose($fp);
$online_array = array();
$file_array = file($file);
foreach($file_array as $newdata){
list($ip, $time) = explode("||", $newdata);
if($time >= $timeout){
array_push($online_array, $ip);
}
}
$online_array = array_unique($online_array);
$online = count($online_array);
if($online == "1"){
echo "Users Online: $online";
}else{
echo "Users Online: $online";
}
?>

Save this one as usersonline.php (it's not the same as the above one. both have different file extensions, so don't confuse one for the other, k.) Be sure you choose "All Files" option from the drop down box when saving so you can save it as a php file and not a text file! Also, make sure you save it where you saved the first one so you can upload it.

Now you'll need to upload both documents in the same directory you have your website pages on that you'd like to display this handy script on. Once you have uploaded both files, chmod usersonline.txt to 777.

Now just copy the following piece of code into your website page where you'd like the number of users online to show up at:

Code:
<?
include("usersonline.php");
?>

 

Domain Names

Sample image .com .info
.mobi .net
.org .tv .us
.biz .ws .name
.am .at .be .cc .cn .de .eu
.fm .jobs .jp .ms .nu .nz
.tc .tw .uk .vg .ASIA .ME

Website Hosting

Sample image We're the affordable, reliable place to host your site. And unlike the competition, there's no set up fee and no annual commitment.

24/7 Support

Sample image Our highly trained, courteous support staff is waiting to help. We'll resolve any issue to your complete satisfaction.