users online without database

Is it possible to detect how many users are currently online on a page (or even better: site) without using a database?

You could, but its easier. Without a db, you’d need to write an entry for each person in a text file, along with a time stamp.

Thanks. I see. But how would they be deleted when they leave the site?

That’s the great debate right there - detecting when someone has left the site. Most just look at the session id on the server and see if its there. Your use would be a little complicated since you’re not actually doing any physical manipulation. I think for this to work, you’d have to record 3 things on 1 line. Something like: username, timestamp, sessionid with 1 line per person. I found this code on a forum, hopefully it can be changed to suit.
// detect sessions on server,
$sids = array();
$sids = session_id();
$ids = array();

if($sids) {

$newsd = file($file); //replace $file with your filename
$cnt = 0;

foreach($sids as $id) {
$ids[] = $id;

foreach ($newsd as $key => $line) {
$key = $key+1;
if ($key != $ids) { $result[] = $line; }

if ($key!= 0) {
$fh = fopen($filed, “w”);
foreach ($result as $nyhet) {
fwrite($fh, $nyhet);
header (“Location: $loc”);
} elseif ($key == 0) {
$fh = fopen($filed, “w”);
fwrite($fh, “”);
For this to work, you have to get the existing sessions ids. From there, you check each line, take out what’s not needed and then rewrite the file each time. Not sure if the id get part will work though, never had to do this before.


This site: also has a script for this. Would it auto-refresh? And does it show the numbers of visitors on my site, or just the visitors on the one page where the script is?

I’m testing it here: Only shows ‘Visitors online: 001’ so far.

Now I’m trying this one:


No luck. Always says 1. I wonder if it’s for the whole site, or just the page it’s on.

Sponsor our Newsletter | Privacy Policy | Terms of Service