So here is a good PHP Script which u can use inorder to show
Users Online Counter or simply
Live Counter for ur Site.
Note :U need a MySQL Database to use this script ....
This script only needs One Table, so u can use ur Database for other purposes as well ...

So here we go .....
First we need to create the Tables in our DATABASE ...
Just run this query, in PHPmyAdmin, or anywhere u like

CODE
CREATE TABLE `live` (
`idx` int(10) unsigned NOT NULL auto_increment,
`ip` varchar(15) NOT NULL default '',
`last_access` int(10) unsigned NOT NULL default '0',
`location` varchar(80) NOT NULL default '',
PRIMARY KEY (`idx`),
KEY `ip` (`ip`),
KEY `last_access` (`last_access`)
) TYPE=MyISAM
After that u have created the tables ...
Save this page as anyname.php
CODE
<?php
// First of all, Connect to Database
$dbhost = 'localhost'; // ur Database Host
$dbuser = 'root'; // ur username
$dbpass = 'password'; // ur password
$dbname = 'databaselive'; // ur database name
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
// gets the IP Address and saves it into Database
$ip = $_SERVER['REMOTE_ADDR'];
$now = time ();
$now_5 = $now - 300; // refresh every 5 min
mysql_query ("DELETE FROM live WHERE last_access < $now_5");
$res = mysql_query ("SELECT * FROM live WHERE ip='$ip' LIMIT 1");
if (!$row = mysql_fetch_array ($res)){
mysql_query ("INSERT INTO live SET ip='$ip', last_access = '$now'");
}
else{
mysql_query ("UPDATE live SET last_access = '$now' WHERE idx='$row[idx]' LIMIT 1");
}
// display the Live Counter
$txt = '';
$n = 0;
$res = mysql_query ("SELECT * FROM live");
while ($row = mysql_fetch_array ($res)){
$n++;
$txt .= "IP: $row[ip] - Last Access: ".date ('r', $row['last_access'])."<BR>";}
$txt = "There are/is $n visitor(s) now:<BR>".$txt;
echo $txt;
?>
Thats it

ur Live Counter is Ready ...
However, u must have seen the Live Counters in Image Format, such as that of Bravenet etc ...
Just like this one ...

No Need to get Confused ...
Its really easy ...
All u need to have is that u should have
GD Library installed in ur Server ....
It is available for free at :
http://www.boutell.com/gd/If u are not sure whether GD Library is installed or not, than u should better ask ur Web Host ...
However there is also another option ....
Just save this file as gd.php or anything.php :
CODE
<?php
if (function_exists('imagecreate')) {
echo "GD Library is enabled <br>rn<pre>";
var_dump(gd_info());
echo "</pre>";
} else {
echo 'Sorry, you need to enable GD library first';
}
?>
Than upload this file to ur Web Server using FTP or anything ...
Point ur browser to this file, and open it ...
Thats it ....
If u see,
GD Library is enabled .... it means that ur Web Server has got GD Library

If u see,
Sorry, you need to enable GD library first ..... it means, that ur Web Server dont have GD Library and this Image Script will not work ....
Anywayzzzz ... Moving further,
Save this file as anyname.php

CODE
<?php
// First of all, Connect to Database
$dbhost = 'localhost'; // ur Database Host
$dbuser = 'root'; // ur username
$dbpass = 'password'; // ur password
$dbname = 'databaselive'; // ur database name
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
header ('(anti-spam-(anti-spam-(anti-spam-(anti-spam-(anti-spam-(anti-spam-content-type:)))))) image/png');
$ip = $_SERVER['REMOTE_ADDR'];
$now = time ();
$now_5 = $now - 300;
// refresh every 5 min
mysql_query ("DELETE FROM live WHERE last_access < $now_5");
$res = mysql_query ("SELECT * FROM live WHERE ip='$ip' LIMIT 1");
if (!$row = mysql_fetch_array ($res)){
mysql_query ("INSERT INTO live SET ip='$ip', last_access = '$now'");
}else{
mysql_query ("UPDATE live SET last_access = '$now' WHERE idx='$row[idx]' LIMIT 1");
}
// display the Count
$res = sql_query ("SELECT COUNT(*) AS num FROM live LIMIT 1");
$row = sql_fetch_array ($res);
$n = $row['num'];if ($n > 1){
$txt = "There are $n visitors now.";
}else{
$txt = "There is $n visitor now.";}
// calculate image size
$w = (strlen ($txt) * 7) + 41;
$h = 17;$gb = imagecreate ($w, $h);
$bg = imagecolorallocate ($gb, 255, 255, 255);
$fg = imagecolorallocate ($gb, 0, 0, 0);
$cy = imagecolorallocate ($gb, 19, 114, 108);
imagerectangle ($gb, 0, 0, $w-1, $h-1, $fg);
imagefilledrectangle ($gb, 2, 2, 35, 14, $cy);
imagestring ($gb, 3, 5, 1, 'LIVE', $bg);
imagestring ($gb, 3, 38, 1, $txt, $fg);
imagePNG ($gb);
imagedestroy ($gb);
?>
CreditsThis article is actually written by me, (Samya Khalid) and is actually a modified version of one available at
PHP Magix. I have added some information, and deleted some information which was redundant and beyond the scope of this topic

You can see the Original Tutorial at :
http://magix.c97.net/read.php?15,1*** All Criticisms and Appreciations are welcome ***
Regards:
Samya Khalid
Comment/Reply (w/o sign-up)