MySQL
  Home arrow MySQL arrow Page 2 - Finding How Many Users Are On Your Site Wi...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Finding How Many Users Are On Your Site With PHP
By: Joe O'Donnell
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 16
    2002-04-27

    Table of Contents:
  • Finding How Many Users Are On Your Site With PHP
  • Building the database
  • Creating the PHP script
  • Displaying the number of users
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More MySQL Articles
    More By Joe O'Donnell


     

    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 ...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT