Keeping track of how many users have been on your website is very easy. This can be done with some basic MySQL code and the date() function. All you have to do is keep track of the users IP address and time they were last on the page. Lets get to some code:
$server = 'localhost'; $user = 'root'; $pass = ''; $db2 = 'online'; $db = mysql_connect($server, $user, $pass) or die("Could not connect."); if(!$db) die("no db"); if(!mysql_select_db("$db2",$db)) die("No database selected."); $server_time=date("U"); $client_ip=$_SERVER['REMOTE_ADDR']; $query = mysql_query("SELECT * FROM current_users WHERE ip='$client_ip'"); $check = mysql_fetch_array($query); if($check) { $update="UPDATE current_users set time='$server_time' where ip='$check[ip]'"; mysql_query($update) or die("Unable to process update: " . mysql_error()); } else { $sql = "INSERT INTO `current_users` (`ip`, `time`) VALUES ('$client_ip', '$server_time')"; $result = mysql_query($sql) or die("Unable to process insert: " . mysql_error()); } $time=$server_time-1800; $remove = "DELETE from current_users WHERE time<'$time'"; // Remove users outside time limit mysql_query($remove) or die("Unable to delete: " . mysql_error()); // Show the count $sql2 = mysql_query("SELECT ip FROM current_users" ); $currentVisitors = mysql_num_rows($sql2); echo 'There have been '.$currentVisitors.' users online in the last 30 minuets.';
The first query checks to see if your IP address is in the database. If your IP address is in the database it updates the time. If your IP address is not in the database, we then insert the time and your IP address.
To eliminate old users we automatically remove users who have been online after 30 minutes(1800 Seconds). If you want to change the time delay just multiply 60 times the number of minutes.
10 Minutes = 600
15 Minutes = 900
20 Minutes = 1200
30 Minutes = 1800
1 Hour = 3600
Make sure you create a database and apply your settings to the variables at the top.
Insert the following SQL query into your database:
CREATE TABLE `current_users` (
`ip` varchar(30) NOT NULL default ”,
`time` varchar(30) NOT NULL default ”
) TYPE=MyISAM;
Just save this as online.php and include this in your page.





















(15 votes, average: 3.80 out of 5)
March 12th, 2008 at 5:42 pm
thanks a lot. Just what I was working for and easy to mod. I had trouble using the create table code above, so I had to create the table manually.
February 25th, 2009 at 10:38 pm
CREATE TABLE `current_users` (
`ip` VARCHAR( 30 ) NOT NULL ,
`time` VARCHAR( 30 ) NOT NULL
) TYPE = MYISAM ;
this table code worked for me.
February 18th, 2010 at 1:41 pm
` you replace with “
August 19th, 2010 at 8:48 pm
replace all ‘
April 30th, 2011 at 9:43 pm
My site is using HTML5… Could this cause the php to not work.
online.php works standalone, but when I do:
No longer works.
April 30th, 2011 at 9:45 pm
Sample code not working:
div id=”onlinecountfooter”>
?php require(“online.php”); ?>
/div>
I took the greater than parts off so I could show you this code.
June 22nd, 2011 at 4:26 am
Most of what you say is supprisingly accurate and it makes me wonder why I hadn’t looked at this in this light before. This piece really did turn the light on for me as far as this topic goes. But there is one point I am not too comfortable with and while I try to reconcile that with the central theme of your point, let me see what the rest of your readers have to say. Well done.
July 15th, 2011 at 2:58 am
xHyperX Mindx, where is your starting php?
?php require………
Next time try “<?php" without quotes.
August 23rd, 2011 at 10:03 am
I am asking for my mother. She doesn’t necessarily want to make money off them, her purpose is to use her blog (once popular) and use it as references to possibly help her get a newspaper article. She has a title for one called “Answers to Life’s Problems”. Where can she post blogs and they become popular? She posted it already on WordPress but there are 3 million people posting blogs hers gets lost in the mix. Any suggestions?.