Computer Programming web Web programming Tips

The simplest page view counter - php code example

By Sergey Skudaev


Happy Amazon Prime Day July 12 - Exclusive Deals for Prime Members - Start Free 30-Day Trial

There are many PHP code examples of visitor counters or page view counters. I show you the simplest page counter PHP code example. You can include this script in every web page and it will display number of times each page was viewed by a user.

create a simple table in MySQL

CREATE TABLE `visitors_count` (
`countid` int(11) NOT NULL auto_increment,
`count` int(11) NOT NULL default '1'
`page` varchar(100) default NULL,
`remoteip` varchar(30) default NULL,
PRIMARY KEY (`countid`)
) ;

Enter 1 as default value for page count field. Page field should contain a web page name, for example, PHP file name. Enter all your page names in the page field. Use query:

"insert into visitors_count (countid, count, page) values(0, 1, 'index');

"insert into visitors_count (countid, count, page) values(0, 1, 'nextpage');

And so on.

If your web page is index.html file, then rename its extension to "php" and type at the bottom:






The counter.php file contains the script that does the job


     $hostname = "localhost";
     $dbuser = "auser";
     $dbpassword = "apassword";
     $dbname = "adatabase";

$db_link=mysql_connect($hostname, $dbuser, $dbpassword) or die("Unable toconnect to the server!");

   mysql_select_db($dbname) or die("Unable to connect to the database");

   $sql="select count from visitors_count where page='".$page."'";




 echo "Visitors:".$count."<br>";


$usql="update visitors_count set count=".$count." where page='".$page."'";

       echo mysql_errno().":";<br>
       echo mysql_error();<br>


How it works?

1. You assign different value for $page variable on each your web page in the script, which you include in the page.

2. query will pull count value for this particular page.

3. Then you display count number

4. Then count variable is incremented by 1 and record updated.

Today, however, you do not need to have any page view counter. Sign up for google analytics and you will see not only how many visitors you have for any your web page, but also you will see where these visitors come from.

Google analytics, 23 visitors from Nigeria

I was surprised by that!

Warning: include(../images/connect.php): failed to open stream: No such file or directory in /hermes/walnaweb12a/b2340/moo.configureallcom/includes/footer.php on line 11 Warning: include(): Failed opening '../images/connect.php' for inclusion (include_path='.:/usr/local/lib/php-5.3.29-amd64/lib/php') in /hermes/walnaweb12a/b2340/moo.configureallcom/includes/footer.php on line 11

Comments (2)

Topic: The simplest page view counter - php code example
4/5 (2)
Full StarFull StarFull StarFull StarEmpty Star
8th July 2016 5:44am
Full StarFull StarFull StarFull StarEmpty Star
very good tutorial nicely explained i learn also from this tutorial and it is with demo you can also learn from this simple and easy
8th March 2016 10:41am
Page 1 of 1

Add Comment

* Required information
(never displayed)
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
What is the sum of 1 + 2 + 3?
Enter answer:
Enter code:
Notify me of new comments via email.
Remember my form inputs on this computer.
I have read and understand the privacy policy. *
I have read and agree to the terms and conditions. *
Powered by Commentics

My eBooks on

Learn C++ Programming By Examples

Learn SQL Programming By Examples

Learn PHP Programming by Examples

Visual Basic Programming By Examples

How to Build Your Own Web Site from Scratch