WhosOnline Tutorial

Hopefully you have all read my short tutorial on PHP and OOP, if not don’t worry too much, you should be fine. We will be covering the basic usage of the WhosOnline class that I created a while back and is pretty popular. I figure I need to get some more info out here to explain it a little better.

Lets start with how it works! First of all, no online counter is 100% accurate. Right now while I am writing this, I am still considered “Online”, but I am not interacting with the server in any way. Lets break it down further. When you access a webpage, the server sends the runs the script you called, and sends you the proper HTML. The server is done with you from that point on, unless you access another script on the webpage and get more info from the server. If I head over to fmylife.com, I type http://www.fmylife.com into my browser and hit enter. Their server runs a script that gets me all the latest FMLs and leaves me alone. I take my sweet time reading and laughing at the 20 or so on the first page, maybe 10 minutes. I would consider myself “online” during that time because I was actively reading postings, but the server doesn’t know that, it only saw me load the page 10 minutes ago. For all it knows, I moved on to another page on a completely different site. So here is the problem, how does the server know IF you are actually online? It doesn’t. So how do we know?!?!?

Basically we just have to keep track of who visited the website and the times they visited. The we can say whether or not they are online. If I have a website with news articles, I would probably say that visitors tend to remain on one page for 20 minutes. If it was a short site, maybe a gaming site, or even a forum, most people will stay on the same page for 5 minutes, 10 minutes max. So anyone that accessed the site in the last 10 minutes would be considered “online” to the server, and we could show it on all of our pages.

So that is how it all works. We have a script that needs to be run on all pages. What it does is checks to see if you have an active session stored in the database. If you do it will just update it with the current time, otherwise it will create a new entry. After it updates the current viewer, it will go through and delete all the “old” and “offline” people (those who have idled 10-15 minutes or so). It does this simply so you don’t fill up your database with thousands of different users and times, which will ultimately slow down your page loading time. Then you just need to have it display who is online!

Lets get down to the nitty-gritty, or the code. The code is well documented/commented and most php editors will give you clues on how to use the functions and classes. First and foremost, you need to be sure you have a mysql database that you can create a table on. The program will create the table automatically if it is allowed, if not you will have to create one for it. There is simply 3 fields, the “sid” or session id. This a “varchar” and I put 100 characters. The next field is the “time” which is an “int” and I gave it 100 as well for the length. The last field is the “users” which is a “varchar” length 100 as well. Even if you don’t plan on using the users, it still needs to be there. If it isn’t, you will get errors galore.

Secondly, we need sessions! A session is like a global variable that works on EVERY page of the server. I can save a persons age on 1 page and access it on the next page quickly and easily. Most people that have some sort of login will use sessions to store whether or not someone is online, and give them access to certain areas if they have a high enough clearance level. All we need a session for is to keep track of them as they surf, so we don’t say 100 people are online if 1 person visits 100 different pages real quick.

So lets get started, first we will start our session and call our class, and then update the table.

1
2
3
4
5
6
7
8
9
10
<?php
  include("whosonline.php");
  session_start();
  //Create your connection
  $who=new WhosOnline('localhost','root','','myDB');
  //Set up the table you will use
  $who->TableSet("online");
  //Update the table with the new data
  $who->Update();
?>

In line 2, we include the code with our class and functions.
In line 3, we start our session (this is typically done at the very beginning, because starting it later on can cause problems)
In line 5, we call our class and store it in the variable $who.

  • localhost – The mysql server (typically localhost)
  • root – Your mysql username
  • ‘ ’ – Your mysql user password goes here
  • myDB – The database name we will connect to

In line 7, we set the name of the mysql table we will use. If that table doesn’t exist, one will be automatically created UNLESS we change it to $who->TableSet("online", false);. By changing it to false, we restrict the function from creating a table if one doesn’t exist. This might be beneficial if you have a limited number of tables/space and have already set one up for it.
In line 9 we simply update the table. This is where it updates the user and deletes idlers.

This previous set of code is what we would include on the header of EVERY page that we want to keep track of. Failure to include it will make people go “offline” if they visit certain pages lacking this intro. Even if you don’t plan on displaying the data, you might still want to include it.

The next part is simply displaying the data. There are 3 ways to do it. The first, and most simple way is to use the Data() function:

1
2
3
4
5
6
7
8
9
10
11
12
<?php
  include("whosonline.php");
  session_start();
  //Create your connection
  $who=new WhosOnline('localhost','root','','myDB');
  //Set up the table you will use
  $who->TableSet("online");
  //Update the table with the new data
  $who->Update();
 
  echo $who->Data();
?>

This is give us the COUNT of active people on our server. You can store it and display however you want.

The second way to display it is with graphical images. I am reminded of those old school counters everyone had on their Geocities webpages, and threw that in just in case people still wanted to do that. In order for that to work, you have to set the images 0-9. You can do a quick google search of numbers and pull up a bunch of different styles to use. I found stupid looking circle ones to use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
  include("whosonline.php");
  session_start();
  //Create your connection
  $who=new WhosOnline('localhost','root','','myDB');
  //Set up the table you will use
  $who->TableSet("online");
  //Update the table with the new data
  $who->Update();
 
  $who->SetImage(0,"http://wikitriz.editme.com/files/Home/Number0.gif");
  $who->SetImage(1,"http://wikitriz.editme.com/files/Home/Number1.gif");
  $who->SetImage(2,"http://wikitriz.editme.com/files/Home/Number2.gif");
  $who->SetImage(3,"http://wikitriz.editme.com/files/Home/Number3.gif");
  $who->SetImage(4,"http://wikitriz.editme.com/files/Home/Number4.gif");
  $who->SetImage(5,"http://wikitriz.editme.com/files/Home/Number5.gif");
  $who->SetImage(6,"http://wikitriz.editme.com/files/Home/Number6.gif");
  $who->SetImage(7,"http://wikitriz.editme.com/files/Home/Number7.gif");
  $who->SetImage(8,"http://wikitriz.editme.com/files/Home/Number8.gif");
  $who->SetImage(9,"http://wikitriz.editme.com/files/Home/Number9.gif");  
  echo $who->DisplayImages();
?>

If I had 124 people online at once, it would show the image for 1, then 2, then 4. In order to display the images, you must use DisplayImages();

The final way requires a bit more work, but it will show WHO is actually online.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
  include("whosonline.php");
  session_start();
  //Create your connection
  $who=new WhosOnline('localhost','root','','myDB');
  //Set up the table you will use
  $who->TableSet("online");
  //Update the table with the new data
  $who->Update();
 
  //For usernames, they must be a variable somewhere.
  $_SESSION['username']="bob";
  $who->UpdateUser('username','SESSION');
  //Update it again (add the username to the session id)
  $who->Update();
  //Show data as an array
  print_r($who->Data("ARRAY"));
?>

In line 12 we set the session variable for the user. This is typically done on a page when they log in, but I put it here to demonstrate. Whatever the username is stored as, we have to set it in the class to get it updated in the database. So in this case, if the variable was stored in the space ‘username’ in the session array, we will update our class in line 13.
Line 13:

  • UpdateUser – The function that tells the class who this user is.
  • ‘username’ – Where the info is stored in the session array
  • ‘SESSION’ – To let the class know to search the session array for that key and then update the class.

Instead of a session variable, you may pull your info from a database and can type $who->UpdateUser($user,'STRING'); where it will simply update the username with a specific variable rather than a value in the session array.
Line 15, we update again, make sure the user name is now associated with his session in the database, so he isn’t just a “Guest” anymore.
Line 17, we use the Data() function again, but this time we want it to return a little more than numbers, so we type “ARRAY” inside to have it return an array with all the data. The array has 4 parts, 0-xxxx numbers are the names of online members. If they are guests, it excludes them from this process. In the ‘count’ key, we have stored the TOTAL number of people online. In the ‘members’ key, we stored how many MEMBERS were online. In the ‘guests’ key, we stored how many GUESTS were online. I sometimes see something like this:

Total online: 14
Guests: 10
Members: 4
Bob, Jo, Steve, Ruben

By using the array, you can display all this pretty easily:

$data=$who->Data("ARRAY");
echo "<table><tr><td>Total online:</td>";
echo "<td>$data[count]</td></tr>";
echo "<tr><td>Guests:</td>";
echo "<td>$data[guests]</td></tr>";
echo "<tr><td>Members:</td>";
echo "<td>$data[members]</td></tr>";
echo "<tr><td colspan="2">";
for($a=0;$a<$data[members];$a++){
   echo $data[$a]."&nbsp;&nbsp;";
}
echo "</td></tr></table>";

Well we did it, we covered the basics. That last little bit was a little rough, with the user names and such, but I think it is pretty self explanatory. There are several advance features that you can customize and use.

One of my favorites and most important is the idle and delete time. To set this, simply set it like a regular variable. Keep in mind it is in SECONDS, so 5 minutes is 300, 10 is 600, 20 is 1200. By default, you are an active member for 10 minutes, and deleted from the database at 20 minutes. If you want to adjust this, simply type: $who->idletime=300; $who->deletetime=600;

Any of the variables can be set in the class itself to make it easier, or by the script calling the class without using the functions. I am planning on updating it a little more in the future to give a few more customizations. If there is anything else you would like to see, let me know. Thanks!