|
|
|
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";
} | | |
|
| usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | | | PostGreSQL and MySQL 2 in 1 db Manager Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL | | | MySQL Class to ease Database connectivity Categories : MySQL, PHP Classes, Databases, PHP | | | MySQL Handler Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes | | | Online Automatic Class Generator for MySQL Tables Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL | | | Specify your connection settings and create a link to a MySQL database. Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides | | | DBOperations - Handles almost all routine mysql queries Categories : PHP, PHP Classes, Databases, MySQL | | | Simple database class Categories : PHP, PHP Classes, MySQL, Databases | | | Simple Mini Poll class library (SimPoll) Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs | | | Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP. Categories : PHP Classes, Databases, PHP, MySQL, InnoDB | | | Ajax PHP Tree (Left and Right) with MySQL Categories : PHP, Databases, MySQL, AJAX, PHP Classes | | | YellowPages Content Grabber (PHP5 +) Categories : PHP, PHP Classes, Regexps, Databases, MySQL | | | Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible Categories : PHP, PHP Classes, Databases, MySQL, MS SQL Server | | | MySQL Connection/Query Class Categories : Databases, MySQL, PHP, PHP Classes | | | Sort the results from a SELECT query (any number of columns) into an array automatically. Categories : PHP, PHP Classes, Arrays, Databases, MySQL | |
| | | | 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.
| |
|
|
|