Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> How To Display XX Users Online On Your Site ?
valcarni
post Mar 2 2006, 04:40 PM
Post #1


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 22
Joined: 2-March 06
Member No.: 11,675



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 thankful

thank you!

This post has been edited by miCRoSCoPiC^eaRthLinG: Mar 3 2006, 01:16 PM
Go to the top of the page
 
+Quote Post
szupie
post Mar 3 2006, 12:47 AM
Post #2


S.P.A.M.S.W.A.T.
Group Icon

Group: Members
Posts: 814
Joined: 22-January 05
From: San Antonio, Texas (No, I'm not dumb. I just moved here...)
Member No.: 2,284



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:

QUOTE
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 traffic
if(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://www.devarticles.com/c/a/PHP/The-Qui...nline-With-PHP/

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.
Go to the top of the page
 
+Quote Post
Kushika
post Mar 4 2006, 11:15 AM
Post #3


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 72
Joined: 3-February 06
From: UK
Member No.: 11,052



I use the following script:
CODE

<a href="http://www.fastonlineusers.com"><script src=http://fastonlineusers.com/online.php?d=www.YOURSITE.com></script> 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. wink.gif
Go to the top of the page
 
+Quote Post
Samya
post Mar 20 2006, 07:57 PM
Post #4


Member [ Level 1 ]
Group Icon

Group: Members
Posts: 35
Joined: 20-March 06
From: Karachi
Member No.: 12,138



QUOTE(Kushika @ Mar 4 2006, 04:15 PM) *

I use the following script:
CODE

<a href="http://www.fastonlineusers.com"><script src=http://fastonlineusers.com/online.php?d=www.YOURSITE.com></script> 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. wink.gif



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 ...
Go to the top of the page
 
+Quote Post
Samya
post Mar 20 2006, 08:38 PM
Post #5


Member [ Level 1 ]
Group Icon

Group: Members
Posts: 35
Joined: 20-March 06
From: Karachi
Member No.: 12,138



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

So here we go .....

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

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

IPB 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 :
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 smile.gif
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 smile.gif

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


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

You can see the Original Tutorial at :
http://magix.c97.net/read.php?15,1

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

Regards:
Samya Khalid
Go to the top of the page
 
+Quote Post
WeaponX
post Mar 21 2006, 03:33 AM
Post #6


Way Out Of Control - You need a life :)
Group Icon

Group: Members
Posts: 1,086
Joined: 21-June 05
From: New York
Member No.: 6,440



For the code that szupie posted above, what path should I give it?
QUOTE
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?
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Site Language(6)
  2. Free Ragnarok Online Server(4)
  3. Accept Payment Online(5)
  4. Free Online Speed Test(13)
  5. A Site I Put Together Over The Last 3 Days(5)
  6. Flash Site Software(12)
  7. What Is The Best Photo Sharing Site?(16)
  8. Free Site(5)
  9. Need To Copy An Entire Site..(7)
  10. The Best Mmorpgs You've Ever Tried!(30)
  11. Free Fast Web Submission(0)
  12. Nice Models And Free Models, New Site!(4)
  13. Outspark's Fiesta Online(2)
  14. Linux Partitioning Guide (new Users)(1)
  15. Warhammer Online: Age Of Reckoning(0)
  1. My Site Got Hacked!(10)
  2. Help Me Host My Site On My Pc(4)
  3. Online Multiplayer Game(16)
  4. Do Google Search Better Than Yahoo?(15)
  5. Free Action Game Online(0)
  6. New Windows Live Messenger 8.5 Beta!(13)
  7. Zoho Writer(5)
  8. Does This Site Mean Anything To Us…i Don’t Know U Tell Me?(4)
  9. Credit System V2.0 Online(17)
  10. Online Board Games(2)
  11. Add A Forum To Your Site(23)
  12. Phishing In Myspace(8)
  13. Disable Display Properties Tabs(3)
  14. How To Make A Text Based Online Game Script ?(23)
  15. How To: Display A Members/user List.(3)


 



- Lo-Fi Version Time is now: 7th October 2008 - 08:58 AM