Home arrow Blog arrow Walk-Throughs arrow Simple Visitor Counter
Border States Consulting, LLC |
Home
Services
Service Plans
Sales
Blog
News Feeds
Links
Advanced Search
Contact
Jobs
Other Menu
Antivirus
Remote Support
Trouble Ticket
Administrator

Authorize.Net Certified Developer

Avast antivirus

 
 
 
Simple Visitor Counter PDF Print E-mail

If you do a quick web search for a php visitor counter, you will find a lot of examples that use flat text files to keep track of the page visits. I prefer to use mysql tables, since the sites I work with are already using a mysql database. Below is a very simple example of how to add a visitor counter to any php website page.

This example only keeps one count and would typically be put on the home page, but you could easily add another field to the visitor_count table to keep separate counters for multiple pages. The visitor_cache table simply records the IP address of the visitor, so that we don't record multiple visits from the same IP. Obviously, you will need to determine how long you want this cache to live, and clear it out on some type of schedule. You may want to clear it every night, so that if the same user comes back tomorrow it will count as a new visit, or you may choose once a week, or once a month - it really depends on your site content and personal preference.

 First, you will need to set up the tables:

--
-- Table structure for table `visitor_cache`
--
 
DROP TABLE IF EXISTS `visitor_cache`;
CREATE TABLE `visitor_cache` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `ip` varchar(128) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
KEY `idx_ip` (`ip`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

--
-- Table structure for table `visitor_count`
--
 
DROP TABLE IF EXISTS `visitor_count`;
CREATE TABLE `visitor_count` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `visitor_count` int(32) unsigned NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

INSERT INTO visitor_count (visitor_count) VALUES (1);

Now we need to add some php code to the page that will display the current visitor count, as well as increment the count if this is a new visitor. You will notice in the code below that I use a class to make the database connection. Your method of connecting to your database will likely be different, but that's an article for another time. For now I'm assuming you know how to connect to your database and submit a query. The nextRow() function of my DBMysqli class just does a fetch_array() on the query result.

$visitor_ip = $_SERVER['REMOTE_ADDR'];
$dbconn = new DBMysqli();
$qry = "SELECT visitor_count FROM visitor_count";
$dbconn->query($qry);
$row = $dbconn->nextRow();
$visitor_count = $row['visitor_count'];
$qry = sprintf("SELECT * FROM visitor_cache WHERE ip = '%s'", $visitor_ip);
$dbconn->query($qry);
if($dbconn->getNumRows() < 1){
   //new visitor
   $visitor_count++;
   $qry = sprintf("UPDATE visitor_count SET visitor_count = %d WHERE id = 1", $visitor_count);
   $dbconn->query($qry);
   $qry = sprintf("INSERT INTO visitor_cache (ip) VALUES ('%s')", $visitor_ip);
   $dbconn->query($qry);
}
echo $visitor_count." Visitors";
$dbconn = NULL;

 Finally you will probably want to style the container where you insert this code, so that you can dress up the text counter a bit. Just wrap it in a div and give it a large readable font family, with a color that complements your page.

Last Updated ( Jun 07, 2016 at 08:56 AM )
 
   
     

 
LEGO® Juniors: Introduce your child to the world of building.

Mambo is Free Software released under the GNU/GPL License. Hosted by <Border States Consulting, LLC>