Home arrow MySQL arrow Page 2 - Finding How Many Users Are On Your Site With PHP
MYSQL

Finding How Many Users Are On Your Site With PHP


Counting the number of users that are currently browsing your site is a great way to brag about your high number of unique visitors if you're running a busy web site. In this article Joe shows us how to use PHP and a MySQL database to show the common "There are currently 23 users online" text on our web site. He uses two very simple scripts to do this, and the result is small, quick and informative. If you run your own site, then be sure to check this article out!

Author Info:
By: Joe O'Donnell
Rating: 5 stars5 stars5 stars5 stars5 stars / 21
April 27, 2002
TABLE OF CONTENTS:
  1. · Finding How Many Users Are On Your Site With PHP
  2. · Building the database
  3. · Creating the PHP script
  4. · Displaying the number of users
  5. · Conclusion

print this article
SEARCH DEVARTICLES

Finding How Many Users Are On Your Site With PHP - Building the database
(Page 2 of 5 )

Our users online script will obviously display how many people are online, but because the HTTP protocol is stateless, so is PHP in a sense. If you're familiar with ASP, then you will know about the global.asa file, which allows you to run events when a user both enters and leaves your site. ASP tracks this with a session timeout, which is 20 minutes by default. When it "detects" that a users session has ended, then code inside a function called session_onEnd() is called.

Unfortunately, because we don't have this kind of functionality available to us with PHP, we're going to have to create a work around to get past it. Here's what we're going to do:
  • We will assume that a session is 20 minutes in length.
  • When a user requests a page from our site, we will check our MySQL table (which we will create shortly) for an entry containing that users IP. If one is found within the last 20 minutes, then we don't add one. On the other hand, if no entry is found in the database, then we add one with the users IP address and current date/time.
  • To display the number of users online, we select a count of all records in our database where the time is within the last twenty minutes.
But first thing's first. Let's create our database. Fire up the MySQL console application and enter the following code:

create database siteStats;
use siteStats;

create table usersOnline
(
id int auto_increment not null,
userIP varchar(20) not null,
dateAdded timestamp,
primary key(id),
unique id(id)
);


Our database contains just one table called usersOnline. UsersOnline contains three fields:
  • id: An automatically incremented ID field, which makes sure each entry in the usersOnline table is unique.
  • userIP: The IP address of the user, i.e. 192.167.4.34.
  • dateAdded: A timestamp of when the user visited our site. It's stored in the format of yyyymmddhhmmss, i.e. 20020428121244. We will use MySQL's UNIX_TIMESTAMP function later to get it into a different format.
When we add a record to our usersOnline table, we only need to specify the IP address, because both the id and dateAdded fields automatically update themselves.

OK, now that we understand how our users online system will work, let's create the simple PHP scripts that will add users to the database and also show how many are online.
blog comments powered by Disqus
MYSQL ARTICLES

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials