PHPBuddy.com
 PHP Function Lookup:
 
Categories
PHP Quick Start
PHP Installation
PHP Articles
PHP Scripts

Top Rated Articles 
  • Simple Ad Rotator
  • PHP and Cookies
  • Getting Screen resolution using JavaScripts & PHP
  • Using Functions in PHP
  • PHP Sessions

  • Site Related
    Submit Articles/Code
    Post your Queries
    Contact Us
    Advertise on this site
    Home

       Home                   Article Added on: April 14, 2002
    Online Users

    Overview: You must have seen on some sites displaying the number of users online, this scripts exactly does that it displays number of users currently browsing your site.

    The table structure

    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)
    );


    User's Online Displays the numbers of users on your website at any one time.

    <?php
    $server = "xxx";
    $db_user = "xxx";
    $db_pass = "xxx";
    $database = "xxx";

    $db = mysql_connect($server, $db_user,$db_pass);
    mysql_select_db($database,$db);

    $timeoutseconds = 300; //5 minutes

    $timestamp = time();
    $timeout = $timestamp-$timeoutseconds;

    $insert = mysql_query("INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')",$db);
    if(!($insert)) {
    print "Useronline Insert Failed > ";
    }
    $delete = mysql_query("DELETE FROM useronline WHERE timestamp<$timeout",$db);
    if(!($delete)) {
    print "Useronline Delete Failed > ";
    }
    $result = mysql_query("SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'",$db);
    if(!($result)) {
    print "Useronline Select Error > ";
    }
    $user = mysql_num_rows($result);


    mysql_close();
    if($user == 1) {
    print("$user user online\n");
    } else {
    print("$user users online\n");
    }
    ?>

        Send this Article to your Friend!
    Your Name Friend's Email
     
    Rate this article:  Current Rating: 2.00
      Poor    Excellent     
              1     2    3    4    5

     

    Home | Privacy Policy | Contact Us | Terms of Service | Submit Articles
    (c) 2002 - 2010 PHPbuddy.com Unauthorized reproduction/replication of any part of this site is prohibited.