WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Simple usersOnline class - keep track of how many users are online on your site
Categories : PHP, PHP Classes, Databases, MySQL Click here to Update Your Picture
Ilir Fekaj
Date : Jan 23rd 2004
Grade : 3 of 5 (graded 14 times)
Viewed : 14337
File : 3798.php
Images : No Images for this code example.
Search : More code by Ilir Fekaj
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

This very simple class enables you to track number of visitors online in an easy and accurate manner. It's free for all purposes, just please don't claim you wrote it. If you have any problems, please feel free to contact me. If you use it, please send me the page URL.

Latest version & info: http://www.free-midi.org/scripts/
Demo: http://www.free-midi.org

Important: You need to create a database connection and select a database before creating the 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 ;



usersOnline.class.php
<?php
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;
    }

}
?>


Example Usage
Example usage of usersOnline class:

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";
}



phpFormGenerator for Dynamic Form Generation from MySQL
Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP
MySQL Connection/Query Class
Categories : Databases, MySQL, PHP, PHP Classes
This class splits the results of the query into multiple pages like what the search engine does.
Categories : PHP Classes, PHP, MySQL, Databases
DBXML- A Class to backup databases in XML Format using web interface
Categories : PHP, PHP Classes, Databases, MySQL, XML
YellowPages Content Grabber (PHP5 +)
Categories : PHP, PHP Classes, Regexps, Databases, MySQL
Ajax PHP Tree (Left and Right) with MySQL
Categories : PHP, Databases, MySQL, AJAX, PHP Classes
Create and restore backup of MySQL databases
Categories : MySQL, Databases, PHP, PHP Classes, Complete Programs
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
MySQL database class
Categories : PHP, MySQL, Databases, PHP Classes
Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP.
Categories : PHP Classes, Databases, PHP, MySQL, InnoDB
TAB_STRUCT Class: Is supporting Class for the DBXML Class
Categories : PHP, PHP Classes, MySQL, XML, Databases
PHP Transfer data from text file to Mysql Table
Categories : PHP, PHP Classes, Filesystem, Databases, MySQL
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
Link Manager for Link Exchangers
Categories : PHP, PHP Classes, Databases, MySQL, CURL
 Linda Kane wrote : 1046
I tried this in a .php3 script and it did not work.
I am a little new.

What am I missing?
 
 Ilir Fekaj wrote : 1047
This class works with PHP 4.1.0 or later.
 
 Ilir Fekaj wrote : 1048
Also make sure you download atached file :)
 
 Frederick Horman wrote : 1049
I wish people would stop posting incomplete examples on the internet!

For those of that want this poor example to work, do the following.

1st create a Database
2nd create the table

then add this to the top of the Online.class.php file:

#Edit the values as needed.
$server = "localhost";
$user = "user";
$pass = "password";
$dbname = "your database name";

mysql_pconnect($server, $user, $pass) or die("Error: " . mysql_error());
mysql_select_db($dbname) or die("Unable to select database"); 

The code will not work until you establish a connection to the database, only then can you query the table for information..
 
 Ilir Fekaj wrote : 1050
This not incomplete example, you need to create database connection which is clearly noted in the script if you read it. Also I assume that people don`t use only users online script on PHP powered pages and are able to create database connections. If this is the only script that you are using on your page, and you don`t know how to create connection, here is official fix:

$host = "localhost"; // db host
$user = "root"; // db username
$pass = ""; // db password
$db = "yourdb"; // db name

$connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect");
mysql_select_db ($db) or die ("Unable to select database");

Also do not create persistent database connections like guy above mentioned mysql_pconnect(), and do not show your users errors mysql_error().
 
 Ilir Fekaj wrote :1051
Also DO NOT put connection parameters in class file, but on page where you`re calling class! That is also clearly noted in instructions.