Jump to content
xisto Community
valcarni

How To Display XX Users Online On Your Site ?

Recommended Posts

i've seen many sites around the net that have something on their page that says like 348 users online... does anyone here know how that is done and could help with this issue.... like i wish to record the number on users on all my 525 pages on my site and add them all together into 1 number that i show on the front page of my site (not just the ppl viewing that 1 page).... if anybody could help i'd be very thankfulthank you!

Edited by miCRoSCoPiC^eaRthLinG (see edit history)

Share this post


Link to post
Share on other sites

I Googled around and found a solution. It requires PHP, which you'll be able to use if you're hosted here. Anyway, here's the code:

 

/* Start the session */session_start();/* Define how long the maximum amount of time the session can be inactive. */define("MAX_IDLE_TIME", 3);function getOnlineUsers(){if ( $directory_handle = opendir( session_save_path() ) ) {$count = 0;while ( false !== ( $file = readdir( $directory_handle ) ) ) {if($file != '.' && $file != '..'){// Comment the 'if(...){' and '}' lines if you get a significant amount of trafficif(time()- fileatime(session_save_path() . '\\' . $file) < MAX_IDLE_TIME * 60) {$count++;}}closedir($directory_handle);return $count;} else {return false;}}echo 'Number of online users: ' . getOnlineUsers() . '<br />';
Source: http://forums.xisto.com/no_longer_exists/

 

The MAX_IDLE_TIME is counted in minutes. Basically, this code starts a session for every visitor of your site, and then, when the page is being processed, it counts the total number of sessions and outputs the number.

Share this post


Link to post
Share on other sites

I use the following script:

<a href="http://http://www.fastonlineusers.com/ src=http://fastonlineusers.com/online.php?d=http://ww38.yoursite.com/; online</a><BR>

I got it from http://www.fastonlineusers.com/ it simple, and only requires a user to have JavaScript enabled in their browser. :o


Hmmm ....
But what you have menioned is something different ...
It is actually a Remotely Hosted Script ...

Which means that all the Scripts are hosted on their Server and we only link them ...

But if u have the support for PHP and MySQL, than I think u should go for a Script on ur own Server ...

I am saying this, because it is a quite observable fact that Script hosted on some other Server will make ur website too slow ...
This is because, data is first transferred from ur Server and than from their Server as well ....

I think that u should go for ur own Script ...

Share this post


Link to post
Share on other sites

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 ... :o

 

So here we go .....

 

First we need to create the Tables in our DATABASE ...

Just run this query, in PHPmyAdmin, or anywhere u like ;)

 

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

 

<?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 minmysql_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 :P

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 ...

 

Posted Image

 

 

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 :

https://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 :

 

<?phpif (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 :P

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 :P

 

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

Credits

 

This 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 :o

 

You can see the Original Tutorial at :

http://forums.xisto.com/no_longer_exists/

 

*** All Criticisms and Appreciations are welcome ***

 

Regards:

Samya Khalid

Share this post


Link to post
Share on other sites

For the code that szupie posted above, what path should I give it?

session_save_path("/path/to/custom/directory");

I don't think my webhost have a dedicated server running...probably a shared.

 

Probably redundant question, but just to be sure, will this count all the visitors in the whole domain/site or just the page with the code in it?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.