valcarni 0 Report post Posted March 2, 2006 (edited) 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 March 3, 2006 by miCRoSCoPiC^eaRthLinG (see edit history) Share this post Link to post Share on other sites
szupie 0 Report post Posted March 3, 2006 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
Kushika 0 Report post Posted March 4, 2006 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. Share this post Link to post Share on other sites
Samya 0 Report post Posted March 20, 2006 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. 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
Samya 0 Report post Posted March 20, 2006 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 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 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 : 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 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 <?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 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
WeaponX 0 Report post Posted March 21, 2006 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