Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


How do I display the number of users currently on my website? by cLFlaVA
Posted: 6 Sep 04 (Edited 8 Dec 05)

This script will show you how to display the number of users that are currently browsing your website.  It is intended to be used with a PHP website that communicates with a MySQL database.

There are three main aspects of this functionality: (1) the MySQL table, (2) the PHP function, and (3) the call to the PHP function.

(1) Creating the MySQL Table - the following SQL code will create the table you need for this functionality:


CREATE TABLE users_online (
  timestamp int(15) NOT NULL default '0',
  ip varchar(40) NOT NULL default ',

(2) The PHP Function - the following PHP code will perform the necessary INSERTS, DELETES, and UPDATES with the users_online table.


// returns number of users currently on the site
function getUsersOnline() {
    # Database-specific information:
    $server = "localhost";
    $database = "db_name";
    $user = "user_name";
    $pw = "password";

    # connect to the database
    $connection = @mysql_connect ($server, $user, $pw) or die ("Error connecting to the database.");

    # select the database
    $db = @mysql_select_db ($database) or die ("Error selecting database.");

    # time limit for users online in seconds - you can increase or decrease this if you want
    # recommended: entries in db older than 1 minute will be removed
    $timeout = 60;

    # get the ip address
    $ip = getenv("REMOTE_ADDR");

    # get the time
    $timestamp = time();
    $timelimit = $timestamp - $timeout;

    # delete expired users
    $sql = "DELETE FROM users_online WHERE timestamp < $timelimit";
    $result = @mysql_query ($sql);

    # insert current user
    $sql = "INSERT INTO users_online (timestamp, ip) VALUES ('$timestamp', '$ip')";
    $result = mysql_query ($sql);

    # if ip already exists, update the timestamp
    if (mysql_errno() == 1062) {
        $sql = "UPDATE users_online SET timestamp = '$timestamp' WHERE ip = '$ip'";
        $result = mysql_query ($sql);

    # get number of users online
    $sql = "SELECT COUNT(*) as num_users FROM users_online";
    $result = mysql_query ($sql);
    $row = mysql_fetch_array($result);
    $num_users = $row['num_users'];
    if ($num_users == 1)
        return "1 user";
        return $num_users . " users";

(3) The Call to the PHP Function - in order to display the number of users online, you must call the function.  Place this code in any page where you would like to display the number of users that are currently on your site.


<? echo getUsersOnline(); ?>

Back to PHP FAQ Index
Back to PHP Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close