Users Online

Category: PHP
Reviewed by: armed rebel   
Reviewed on: Jul 05 2006
» Discuss this topic ( Posts)

If you've ever needed a guest/member counter for a website, then read this article and you'll be set. This article will go though setting up your MySQL database, adding records to you database and pulling those results out to display them. Note: This article assumes that you

  • have a users table
  • have a way to reference users by a unique ID
  • use sessions to determine if a user is a member

Intro and Editing your Database

Before we start, we should get a general idea of what we are trying to accomplish. In the end, when will have two displayed numbers, and one list. One counter will be for any guests browsing the site and secondly any members browsing the site. Then, we will list all the members on the site. For the MySQL entries, we are going to have one "users_online" table, then rest of the details can be found later in the page. As for getting/updating the data, firstly we'll collect the user data into variables for easier input into the database. Next, we'll be checking that info against the database to determine if the user has been registered as online - if s/he is, then we update the information in the database, if not, then we input the first set of data into the database, and then we update it further. Finally we'll pull the information out of the database and display it.

Although you may edit the code as you wish, it is suggested that you keep all code as it is, and edit it after so there is little room for error.

The first step in creating a users online count is having a table to store and get the information from. Use the following MySQL query on your database of choice:

CREATE TABLE `users_online` (
`session_id` VARCHAR( 32 ) NOT NULL ,
`last_active` VARCHAR( 10 ) NOT NULL ,
`guest` ENUM( '0', '1' ) DEFAULT '1' NOT NULL ,
`user_id` BIGINT,
`ip_address` VARCHAR( 15 ) NOT NULL ,
PRIMARY KEY ( `session_id` ) ,

Use your favorite method to run this query into your database, and continue.

(Note: If you feel you have an understanding of the above query, move on to the next page.)

This is a pretty basic query, so I won't go to in depth into it, but I'll run over a few key details.

`session_id` VARCHAR( 32 ) NOT NULL , The length of session ids is always 32, so we'll put that in a VARCHAR data type with a length of 32.

`last_active` VARCHAR( 10 ) NOT NULL , The length of all UNIX Timestamps are 10 digits, and will continue to be for the few centuries, so we are safe with 10. We could also use the INT data-type, but VARCHAR is fine. This same logic applies to the ip_address field as well, IP Addresses are all 15 characters in length.

`guest` ENUM( '0', '1' ) DEFAULT '1' NOT NULL , The guest type uses the data-type ENUM. ENUM is basically a list of options, and only one option can be selected. Here, we are just using a simple True/False (1/0) list. The default is set to True (1), and you'll see why later in the tutorial.

(Note: For more information on MySQL Data-types, visit this page.)

In the next step, we'll be storing and updating your user data.