KingPin's Forum
 
K.P.s.N. Register vbBux / KPs Mall Bugs Blogs FAQ Search Today's Posts Mark Forums Read Donate
Go Back   KingPin's Forum > KP's Network Forum > Tutorials & Stuff!
Reload this Page PHP Tutorial #5 : Users Online
 


Tutorials & Stuff! This is a discussion on PHP Tutorial #5 : Users Online in the Tutorials & Stuff!;
Description: Ever wanted to know how many users are online at a time and u see how the popular forum software ...

Reply
 
LinkBack Thread Tools
PHP Tutorial #5 : Users Online
(#1 (permalink))
Old
KingPin's Avatar
KingPin is Offline
Da Boss!
KingPin has disabled reputation
 

My Mood:
 
Posts: 7,034
Thanks: 20
Thanked 17 Times in 15 Posts
Blog Entries: 22
Join Date: Apr 2004
Location: Brooklyn, NY
PHP Tutorial #5 : Users Online - July 10th, 2004

Ever wanted to know how many users are online at a time and u see how the popular forum software already has this... well there is a basic idea of this class:
Code:
<?php

/*



Example usage:



include_once ("usersOnline.class.php");

$visitors_online = new usersOnline;



if ($visitors_online->count_users() == 1) {

  echo "There is " . $visitors_online->count_users() . " visitor online";

}

else {

  echo "There are " . $visitors_online->count_users() . " visitors online";

}



Important: You need to create database connection and select database before creating object!

--------------------------------------------

Table structure:

CREATE TABLE `useronline` (

 `id` int(10) NOT NULL auto_increment,

 `ip` varchar(15) NOT NULL default '',

 `timestamp` varchar(15) NOT NULL default '',

 PRIMARY KEY (`id`),

 UNIQUE KEY `id`(`id`)

) TYPE=MyISAM COMMENT='' AUTO_INCREMENT=1;



*/



class usersOnline {



  var $timeout = 600;

  var $count = 0;

  

  function usersOnline () {

    $this->timestamp = time();

    $this->ip = $this->ipCheck();

    $this->new_user();

    $this->delete_user();

    $this->count_users();

  }

  

  function ipCheck() {

  /*

  This function checks if user is coming behind proxy server. Why is this important?

  If you have high traffic web site, it might happen that you receive lot of traffic

  from the same proxy server (like AOL). In that case, the script would count them all as 1 user.

  This function tryes to get real IP address.

  Note that getenv() function doesn't work when PHP is running as ISAPI module

  */

    if (getenv('HTTP_CLIENT_IP')) {

      $ip = getenv('HTTP_CLIENT_IP');

    }

    elseif (getenv('HTTP_X_FORWARDED_FOR')) {

      $ip = getenv('HTTP_X_FORWARDED_FOR');

    }

    elseif (getenv('HTTP_X_FORWARDED')) {

      $ip = getenv('HTTP_X_FORWARDED');

    }

    elseif (getenv('HTTP_FORWARDED_FOR')) {

      $ip = getenv('HTTP_FORWARDED_FOR');

    }

    elseif (getenv('HTTP_FORWARDED')) {

      $ip = getenv('HTTP_FORWARDED');

    }

    else {

      $ip = $_SERVER['REMOTE_ADDR'];

    }

    return $ip;

  }

  

  function new_user() {

    $insert = mysql_query ("INSERT INTO useronline(timestamp, ip) VALUES ('$this->timestamp', '$this->ip')");

  }

  

  function delete_user() {

    $delete = mysql_query ("DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");

  }

  

  function count_users() {

    $count = mysql_num_rows ( mysql_query("SELECT DISTINCT ip FROM useronline"));

    return $count;

  }



}

?>

 
Reply With Quote
Revenue Shared Ads
PHP Tutorial #5 : Users Online
Revenue Shared Ads
Reply

Bookmarks

Tags
php, tutorial, users, online

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Rupees Per Thread View: 1.00
Rupees Per Thread: 15.00
Rupees Per Post: 5.00
Forum Jump



These are the 100 most searched terms
Search Cloud
_backup.rc antrix wow antrix wow server autocad 2007 keygen best google gadgets best wow character bt map devil may cry 4 crack enchanting leveling erika bella fuckteam fuckteam 5 georgina lempin gigistar gigistar topless kpsden kpsforum leatherworking guide leveling enchanting mangos custom vendors mangos item id mangos linux mangos server mangos spell id mangos wotlk mature creampie milena velba no rest for the ass object id wow phoenix marie this is sparta vipersdenforums voyage century bot windows activation workaround windows xp lite wotlk alpha private server wotlk private server wotlk server wotlk wiki wow leatherworking guide wow mount locations wow object id wow object id list wow object id's wow object ids wow objects id www.adobeflashplayer www.adobeflashplayer.com www.kpsforum.com xbox 360 mmorpg ... powered by Simple Search Cloud
Powered by vBulletin® Version 3.8.0 Beta 3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright 2004-2009 KPsN


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82