I just coded a small script to display the number of users currently online on your site. SO i thought to share it here. Here it goes. Create a small table in any database you already have or you may create a new database also Now change the table, database name, database password and host name etc. in the following code and place it wherever you want the "XX Visitors Online" message to appear in the script. Infact, for good results it must execute every time a page on the site is loaded. So better place in the overall footer of your site. <? $server = "localhost"; $table = "visitors"; $database = "xxxx"; $username = "xxxx"; $password = "xxxx"; $connection = mysql_connect($server,$username,$password); mysql_select_db($database,$connection); $minutes = 5; $seconds = $minutes*60; $past = time()-$seconds; $now = time(); if ( mysql_num_rows(mysql_query("SELECT * FROM $table WHERE ip='$_SERVER[REMOTE_ADDR]'")) != 0 ) { mysql_query("UPDATE $table SET date='$now' WHERE ip='$_SERVER[REMOTE_ADDR]'") or die ("Error - Unable to update visit"); } else { mysql_query("INSERT INTO $table VALUES ('$_SERVER[REMOTE_ADDR]','$now')") or die ("Error - Unable to insert records"); } mysql_query("DELETE FROM $table WHERE date < $past") or die ("Error - Unable to delete old visits"); $visitors = mysql_result(mysql_query("SELECT COUNT(*) FROM $table"),0); php echo ("<b>" . $visitors . "</b> guests online"); ?> PHP: And you are done
Thx, but you bether get the variables out of the quotes, example: mysql_query("DELETE FROM $table WHERE date < $past") or die ("Error - Unable to delete old visits"); becomes mysql_query("DELETE FROM `".$table."` WHERE `date` < ".$past."") or die ("Error - Unable to delete old visits");
Nice work aditya sfs.. I have been using an iframe on mine. I think your code to be better and will try it out soon.. Thanks
Nice work. I actually did a more complex one that shows what pages users are on and when did they access it. www.jstracker.com ( the report here www.jstracker.com/sites.php ) Technically, it's an off-site script, only 1 line of javascript for you and the rest is done on my machine. Green rep though for you.
As a friendly suggestion...try encapsulating with either functions or as a class. This will make it easier to maintain, extend, and also pass around.
I want to ask another question here whether there is any other script to display how may users from each country
To limit the queries, you could run the update query at first, and use the insert query only when mysql_affected_rows() returns 0. Something like that: mysql_query("UPDATE $table SET date='$now' WHERE ip='$_SERVER[REMOTE_ADDR]'") or die ("Error - Unable to update visit"); if (mysql_affected_rows() == 0) { mysql_query("INSERT INTO $table VALUES ('$_SERVER[REMOTE_ADDR]','$now')") or die ("Error - Unable to insert records"); } PHP:
so jstracker is yours... hmmm, i saw it at the new site i bought> 4july.info aditya. thanks for the effort
Great man. I was just browsing around your site. Looks simple for the users. But i can guess the complexity of the code behind
i tried to make it simple for the users. no registration they just get the code to plug. The backend is of course a bit more complex but it's not that complex as you might think. I started small thens tarted adding stuff as time passed by such as the live report.
Hiya, been using for a night or two... tonight yahoo and google bots visited my site. Needless to say, currently, I show 76 visitors (3 actual people). Is there a way to plug some code in to ignore certain IP ranges? To ignore things like googlebot and such. Thanks