1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Showing no. of users on your site using PHP/MySQL

Discussion in 'PHP' started by pradeep, Jul 31, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    How to show the no. of users online on your site using PHP & MySQL.

    Step1: Creating the database
    Create a new table called 'useronline', with 3 fields inside a table. The fields are timestamp, ip and file. Below is the SQL to create the table.

    Code:
    CREATE TABLE useronline (
     timestamp int(15) DEFAULT '0' NOT NULL,
     ip varchar(40) NOT NULL,
     file varchar(100) NOT NULL,
     PRIMARY KEY (timestamp),
     KEY ip (ip),
     KEY file (file)
     );
    Step 2: The PHP Script

    PHP:
    <?php
     
    //Put your basic server info here
     
    $server "localhost"//normally localhost
     
    $db_user "username"//your MySQL database username
     
    $db_pass "password"//your MySQL database password
     
    $database "users";
     
    $timeoutseconds 300;
     
     
    //this is where PHP gets the time
     
    $timestamp time();
     
    $timeout $timestamp-$timeoutseconds;
     
     
    //connect to database
     //$server = localhost probably
     //$db_user = your MySQL database username
     //$db_pass = //your MySQL database password
     
    mysql_connect($server$db_user$db_pass);
     
     
    //insert the values
     
    $insert mysql_db_query($database"INSERT INTO useronline VALUES
     ('
    $timestamp','$REMOTE_ADDR','$PHP_SELF')");
     if(!(
    $insert)) {
     print 
    "Useronline Insert Failed > ";
     }
     
     
    //delete values when they leave
     
    $delete mysql_db_query($database"DELETE FROM useronline WHERE timestamp<$timeout");
     if(!(
    $delete)) {
     print 
    "Useronline Delete Failed > ";
     }
     
     
    //grab the results
     
    $result mysql_db_query($database"SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'");
     if(!(
    $result)) {
     print 
    "Useronline Select Error > ";
     }
     
     
    //number of rows = the number of people online
     
    $user mysql_num_rows($result);
     
     
     
    //spit out the results
     
    mysql_close();
     if(
    $user == 1) {
     print(
    "$user user online\n");
     } else {
     print(
    "$user users online\n");
     }
     
    ?>
     
    The script basically works like this. It receives server information and gets the time. In MySQL it fills in the values of the person online and puts up a message if it fails. After a timeout period MySQL deletes the user i.e. we assume that the user is not on our site anymore.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    You could probably optimize the performance if you put the delete in a cron jobs rather than an online query which executes each time a page is loaded.
     

Share This Page