Home arrow Blog arrow Walk-Throughs arrow Simple Visitor Counter
Border States Consulting, LLC |
Service Plans
News Feeds
Advanced Search
Other Menu
Remote Support
Trouble Ticket

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`)

-- 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`)

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. Assuming you've assigned the variables properly for the mysqli instantiation...

$conID = new mysqli($dbServ, $dbUser, $dbPass, $dbName);
    trigger_error('Failed to connect: '.$conID->connect_error, E_USER_ERROR);
    die('Process died');
$visitor_ip = $_SERVER['REMOTE_ADDR'];
if(filter_var($visitor_ip, FILTER_VALIDATE_IP)){
    $result = $conID->query("SELECT visitor_count FROM visitor_count");
    $row = $result->fetch_array();
    $visitor_count = $row['visitor_count'];
    $qry = "SELECT * FROM visitor_cache WHERE ip = '". $visitor_ip ."'";
    $result = $conID->query($qry);
        //new visitor
       $qry = "UPDATE visitor_count SET visitor_count = ". $visitor_count ." WHERE id = 1";
       $qry = "INSERT INTO visitor_cache (ip) VALUES ('". $visitor_ip ."')";
echo $visitor_count." Visitors";

 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 ( Nov 15, 2017 at 05:56 AM )

Take the iconic LEGO MINI Cooper for a drive!

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