Tutorial: PHP/mySQL Simple User Counter
Posted by Pat
Today I am going to show you how to make a simple user counter with total and unique hit statistics. This is just a simple script tutorials so you will be able to build upon it.
Let set up our database structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php // This is how your DB will be setup. /* CREATE TABLE IF NOT EXISTS `visitors` ( `visitor_id` int(11) NOT NULL auto_increment, `name` varchar(225) NOT NULL default '', `value` varchar(225) NOT NULL default '', PRIMARY KEY (`visitor_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; INSERT INTO `visitors` (`visitor_id`, `name`, `value`) VALUES (1, 'ip', ''), (2, 'hits', '0,0'); */ ?> |
Now to call the database in conf.inc.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php $db_user = ""; // Username $db_pass = ""; // Password $db_database = ""; // Database Name $db_host = ""; // Server Hostname $db_connect = mysql_connect ($db_host, $db_user, $db_pass); $db_select = mysql_select_db ($db_database); // Code to pull the results from the db. $q = mysql_query("SELECT * FROM `visitors` WHERE `name` = 'hits'"); // Finds the row with the numbers $visitor = mysql_fetch_array($q); $visitor = split(",", $visitor['value']); // Splits the query result into an array. define('SITE_HITS', $visitor['0']); // Defines a constant for all hits define('SITE_UNI_HITS', $visitor['1']); // Defines a constant for unique hits function visitor($ip) { // Function that will be put on main page. $q_ip = mysql_query("SELECT * FROM `visitors` WHERE `name` = 'ip'") or die(mysql_error()); // Finds the info for all of the ip's $r_ip = mysql_fetch_array($q_ip); // Fetches the ip array we will use later. $array_ip = split("-", $r_ip['value']); // Splits the results and turns it into an array of all the ips $q_hit = mysql_query("SELECT * FROM `visitors` WHERE `name` = 'hits'") or die(mysql_error()); // Finds the info for the hits $r_hit = mysql_fetch_array($q_hit); $hit = split(",", $r_hit['value']); // Splits the query results into an array. $hits = $hit['0']; // The total hits $uni = $hit['1']; // The unique hits if (in_array($ip, $array_ip)) { // If the users ip is in the array we defined earlier $hits++; // Adds a hit, but not a unique hit } else { $hits++; // Adds both a unique and hit because he is a new user. $uni++; $ip = $r_ip['value'] . $ip; // Adds the new ip to the ips already in the db mysql_query("UPDATE `visitors` SET `value` = '$ip' WHERE `name` = 'ip'"); // Updates the table } $hits = $hits . "," . $uni; // Combines the hits and unique hits mysql_query("UPDATE `visitors` SET `value` = '$hits' WHERE `name` = 'hits'"); // Updates the table } ?> |
Breakdown:
First we set the db info.
Next we define the hit/unique constants to display it on the stats page.
After we create the function visitors to add the users ip and hits into the db.
Now the code you would put in the index.php or the page you want the stats for.
1 2 3 4 5 | <?php include("conf.inc.php"); visitor($_SERVER["REMOTE_ADDR"]); // Use the function we defined earlier with the users ip. echo "Main page"; ?> |
Breakdown:
First we include the conf.inc.php for the db info and the functions.
Next we initiate the function visitors with the users ip.
Now for the stats.php page:
1 2 3 4 5 6 | <?php include("conf.inc.php"); echo "Total Hits: " . SITE_HITS; echo "<br />"; echo "Unique Hits: " . SITE_UNI_HITS; ?> |
And thats it, I hope you build off it. If you have any questions please feel free to post. Thanks
July 24th, 2008 at 9:59 am
Thank u i was just looking for this.
September 18th, 2008 at 3:36 pm
You should change:
$ip = $r_ip['value'] . $ip;
to
$ip = $r_ip['value'] . ‘-’ . $ip;
Otherwise, the uni hits won’t be correct..
//Greef
January 8th, 2009 at 10:17 am
You know, this could be done way more simple and much cleaner than this. You made my day tho, never seen a more complicated “counter” (that doesnt work).
Consider this.
ip_table:
+—+————-+
|id | ip (unique) |
+—+————-+
Now, if you try to insert the ip and it exists it will fail, so no new ip gets inserted.. magic!
To count all your _unique_ visitors you have this funny sql, select count(ip) from ip_table. Damn wasnt that simple?
February 5th, 2009 at 6:54 am
Nice Dude, thanks a lot, work fine here. I put this with a include in my site, if a want to show the “rec Online Users” how do i do in the code?
October 31st, 2009 at 7:20 pm
what do you name the piece of code
October 31st, 2009 at 7:30 pm
never-mind about that. anyway this thing works perfectly the other guys are not just doing it right