MySQL
  Home arrow MySQL arrow Page 4 - 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 
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 / 17
    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 - Displaying the number of users


    (Page 4 of 5 )

    At this point some of you may be questioning the method that we’re using to grab the number of users from the database, however, coming from an ASP background, I've used what I know about ASP to accomplish this, and as far as I and many other people are concerned, the statelessness of HTTP sucks. If there was some way to tell PHP when a session starts and ends, then this task would be made so much easier, but because we don’t we're making the best of what we have: a stateless protocol, a database, and the users IP address.

    Anyhow, create a new file called usersonline.php and enter the following code into it:

    <?php

    include("dbinfo.php");

    // Set length of session to twenty minutes
    define("SESSION_LENGTH", 20);

    $sConn = @mysql_connect($dbServer, $dbUser, $dbPass)
    or die("Couldnt connect to database");

    $dbConn = @mysql_select_db($dbName, $sConn)
    or die("Couldnt select database $dbName");

    $timeMax = time() - (60 * SESSION_LENGTH);

    $result = mysql_query("select count(*) from usersOnline where unix_timestamp(dateAdded) >= '$timeMax'");

    $usersOnline = mysql_result($result, 0, 0);

    echo "There " . ($usersOnline != 1 ? "are" : "is") . " $usersOnline user" . ($usersOnline != 1 ? "s" : "") . " online. ";

    if($usersOnline == 0)
    echo "It's not too busy at the moment.";
    else if($usersOnline >= 1 && $usersOnline <= 10)
    echo "Users are starting to build up.";
    else if($usersOnline >= 11 && $usersOnline <= 30)
    echo "Wow, it's getting packed in here!";
    else if($usersOnline >= 31 && $usersOnline <= 50)
    echo "The servers are running on full speed ahead!";
    else
    echo "Wow!!! This site is packed!";

    ?>


    The first eight lines or so are exactly the same as adduser.php, i.e. we're still connecting to the database, defining SESSION_LENGTH, etc. After that we get a count of all users who a record was added for in the last twenty minutes or less:

    $result = mysql_query("select count(*) from usersOnline where unix_timestamp(dateAdded) >= '$timeMax'");

    We then use mysql_result to get the number of users into $usersOnline and print out how many users are online. We set the syntax of the statement so that if there is 1 user online then "There is 1 user online" will be printed instead of "There are 1 users online":

    $usersOnline = mysql_result($result, 0, 0);

    echo "There " . ($usersOnline != 1 ? "are" : "is") . " $usersOnline user" . ($usersOnline != 1 ? "s" : "") . " online. ";


    Lastly, we output a comment, depending on how many users are online:

    if($usersOnline == 0)
    echo "It's not too busy at the moment.";
    else if($usersOnline >= 1 && $usersOnline <= 10)
    echo "Users are starting to build up.";
    else if($usersOnline >= 11 && $usersOnline <= 30)
    echo "Wow, it's getting packed in here!";
    else if($usersOnline >= 31 && $usersOnline <= 50)
    echo "The servers are running on full speed ahead!";
    else
    echo "Wow!!! This site is packed!";


    If you're feeling creative then you should change these messages to maybe include some humor or make them relate to your site somehow.

    Once I got a couple of work colleagues to visit my page, here's how it looked:

    Our visitor counting script in action!

    To use our scripts, simple include adduser.php at the top of every page on your site like this:

    <?php include("adduser.php"); ?>

    Then, wherever you want to display how many users are online, just include usersonline.php, like this:

    <?php include("usersonline.php"); ?>

    Of course, the support material for this article contains all of our scripts as well as the MySQL code to re-create our database and table. You can download it on the next page.

    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-2010 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek