Active Users (PHP Script)

Warning: this is an old article (originally written in and around 2007, I believe).

A member of a forum I frequent needed an active users online script so I wrote up the below for him. It’s an overdone and unoriginal script but maybe someone will find it useful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
 
$CONFIG = array ();
$CONFIG["visitor_file"] = "visitors.txt";
$CONFIG["timeout"] = 60;
 
if (file_exists($CONFIG["visitor_file"])) {
  $curData = unserialize(file_get_contents($CONFIG["visitor_file"]));
  $curTime = time();
 
  $newData = array();
  $userInFile = false;
 
  if (count($curData) > 0) {
    foreach ($curData as $ip => $timeOfLastVisit) {
      if ($curTime - $timeOfLastVisit >= $CONFIG["timeout"]) {
        continue;
      }
 
      if ($ip == $_SERVER["REMOTE_ADDR"]) {
        $userInFile = true;
        $newData[$ip] = time ();
      } else {
        $newData[$ip] = $timeOfLastVisit;
      }
    }
  }
}
 
if (!isset ($newData) || count ($newData) == 0 || (isset ($userInFile) && !$userInFile)) {
  $newData[$_SERVER["REMOTE_ADDR"]] = time ();
}

Bob Hensley

IT Manager | Webmaster | Social Media Strategist | SEO Specialist | Copywriter at Fairfield's Auto Group
Bob Hensley is an IT Manager, Webmaster and all-around advertising guy by day; database theory junkie by night.Born and raised in New England, he lives just a couple hours outside of Boston with his lovely wife and one year old son.He's a technologist, and thoroughly enjoys finding new ways to use technology to better efficiency and work flows.

Latest posts by Bob Hensley (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *

*