hi guys i'm using following script to show online users on my site: http://fonebuzz.net <?php /* usersOnline.class.php Author: Ilir Fekaj Contact: tebrino@hotmail.com Last updated: July 28, 2005 Version: 1.1 Latest version & info: http://www.sim-php.info Support: http://forum.sim-php.info/ (if you find bugs or you need help with installation) Demo: http://www.free-midi.org 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. Also if you like this script please put link to http://www.sim-php.info. Thanks Simply paste this code where you wish your users online count to appear: include_once ("index.php"); $visitors_online = new usersOnline(); if (count($visitors_online->error) == 0) { 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"; } } else { echo "<b>Users online class errors:</b><br /><ul>\r\n"; for ($i = 0; $i < count($visitors_online->error); $i ++ ) { echo "<li>" . $visitors_online->error[$i] . "</li>\r\n"; } echo "</ul>\r\n"; } Important: You need to create database connection and select database before creating object! Example connection would look like this: $host = "localhost"; // your MySQL host i.e. the server on which the database is, usually localhost $user = "attaulla_fbuzz"; // your MySQL username $pass = "0000"; // your MySQL password $db = "attaulla_fbuzz"; // the database to which you're trying to connect to $conn = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect to database."); mysql_select_db("$db", $conn); -------------------------------------------- Table structure (paste this code in PHPMyAdmin or whatever program you use for db management): 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 ; */ class usersOnline { var $timeout = 600; var $count = 0; var $error; var $i = 0; function usersOnline () { $this->timestamp = time(); $this->ip = $this->ipCheck(); $this->new_user(); $this->delete_user(); $this->count_users(); } function ipCheck() { /* This function will try to find out 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')"); if (!$insert) { $this->error[$this->i] = "Unable to record new visitor\r\n"; $this->i ++; } } function delete_user() { $delete = mysql_query ("DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)"); if (!$delete) { $this->error[$this->i] = "Unable to delete visitors"; $this->i ++; } } function count_users() { if (count($this->error) == 0) { $count = mysql_num_rows ( mysql_query("SELECT DISTINCT ip FROM useronline")); return $count; } } } ?> PHP: check at bottom of my site, it always shows 1 user, i tried a lot. I also asked few of my friends to visit my site at same time, still shows 1 visitor online. Please tell me where is the problem? More: I added the bottom file as include Like: Myindex.php i added footer.php in footer.php the include of that script.. any suggestion.??
It's probably as simple as when you look at the script, there is one person online - namely YOU ... or do you mean the number never increases ?
no it doesn't i almost tried to access site with 4 users but the number never increases. script file = online.php footer file = footer.php (included online.php here) main file = index.php (included footer.php here)
Have you checked that the users are being added to the useronline table. I would suggest checking the contents of the table when you have more than one user logged in. Brew
well its not sessioned script. I'm just counting all users who are currently online: I've changed the script now but still the same error: new script <? /** * * TG WHO'S ONLINE * Copyright 2005 - 2006 (c) TOXIC GOBLIN * http://www.toxicgoblin.com * */ //Optional Database Connection Information //**Uncomment the following 2 lines and edit the values if you do not already have an active database connection** // $db = mysql_connect("localhost", "attaulla_online", "0000") or die("Could not connect"); mysql_select_db("attaulla_online"); //Fetch Time $timestamp = time(); $timeout = $timestamp - 900; //Insert User $insert = mysql_query("INSERT INTO TG_whos_online (timestamp, ip, file) VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')") or die("Error in who's online insert query!"); //Delete Users $delete = mysql_query("DELETE FROM TG_whos_online WHERE timestamp<$timeout") or die("Error in who's online delete query!"); //Fetch Users Online $result = mysql_query("SELECT DISTINCT ip FROM TG_whos_online") or die("Error in who's online result query!"); $users = mysql_num_rows($result); //Show Who's Online if($users == 1) { print("$users Visitor Online \n"); } else { print("$users Visitors Online \n"); } ?> PHP: included this to my footer file, but still the same. here is the table created: CREATE TABLE `TG_whos_online` ( `id` bigint(20) NOT NULL auto_increment, `timestamp` int(15) NOT NULL default '0', `ip` varchar(40) NOT NULL default '', `file` varchar(100) NOT NULL default '', PRIMARY KEY (`id`), KEY `ip` (`ip`), KEY `file` (`file`), KEY `timestamp` (`timestamp`) ) TYPE=MyISAM; Code (markup):
Have you checked the database to make sure that the entries are being added properly? Make sure there is more than one entry in the table.
well no error i found in the script. Yes, you are right the entry in mysql is not entered correctly.. What the problem i see is the "IP" if i enter some ip myself then it shows 2 users but that is totally manual.. The problem with it is that it doesn't add new ip in database to show current online users..
After all of your mysql_query() statements, place: OR die(mysql_error());, and see if that gives us a hint. mysql_query("SQL string here") OR die(mysql_error()); PHP:
well i have OR die(mysql_error()); after each mysql query.. Near me, mysql is working fine. I think their is something wrong with IP fetcher.. Whenever someone visits the script doesn't update the ip..
guys !! i have tried almost the third script which runs as same showing 1 user online all the time but still the same problem. This means the script has nothing wrong but my site. I think something's gone wrong with my site or included files.. messing a lot but no solution.. one more thing: is .htaccess has any effect or not? coz i changed the .htaccess rewrite so my site moves from www.fonebuzz.net to fonebuzz.net.. any ideas???