PDA

View Full Version : How do you display how many people are browsing your website?


viper21
01-25-2006, 05:45 PM
Just want to have on my page "10 people are browsing right now" type of deal... How do you do it? Is it simple?

devinemke
01-25-2006, 06:53 PM
<?php
echo '10 people are browsing right now';
?>

sorry, i couldn't resist. but seriously, i assume you already have some sort of login system in place? if so then you will need to store login/logouts in your database and then do a COUNT() query for all current active logins.

Element
01-25-2006, 07:55 PM
i believe this would do it:


<?php

if(isset($_SERVER['REMOTE_ADDR'])) {
$tracker = "tracker.txt";
if(is_writable($tracker)) {
$handle = fopen($tracker, "w+");
$insert = $_SERVER['REMOTE_ADDR'] . "\n";
if(!(fwrite($handle, $insert, strlen($insert)))) {
die("Unable to store logs, page haulted.");
}
} else {
die("Log file is not writable.");
}
}

$tracker = "tracker.txt";
$logs = file($tracker);
$online_num = count($logs);
echo "There are cururently " . number_format($online_num) . "'s users online.";

?>


There is an example, however please note you need to add a checker, and delete and make a new tracker every 5 minutes or so to make sure it refreshes and starts counting the users browsing again fresh.

Its a limited method, but I am assuming you don't have a login system and just want to count how many people are surfing.

There is a tutorial using sessions here (http://www.devarticles.com/c/a/PHP/The-Quickest-Way-To-Count-Users-Online-With-PHP/1/) that is more acurate.