PHP Page Views Count Example

Every website or blog is needed page count to show the top visiting pages. It helps visitors to look around the interesting
posts and for analysis.

Some methods are using for this process,

Main Table Views Column

A simple Update of view count column in Main table for all Dynamic pages is very easy to handle. But if visitors reload
 or come back again to the page duplicate view count will be updated. Then, the Page views count is Inaccurate.

Main Table
Id post views
101 Content of the first post 546
102 Content of the second post 134
103 Content of the third post 594

Seperate Views Table

Have to create a DB Table and make entry of each page with the visitors IP. It is accurate, but our table size will become
very large.

For instance, A growing small website have 2000 pages with average of 1000 unique page visits gets,
2000*1000 = 2,000,000 million entries.

View Table
Id page_url visitor_ip

This is huge number, while the website grows fetching of views count will slow down the page load. And updating Views table
 each time have to check this Ip and this URL made an entry else Insert a new row.

To avoid this, add a new Column views in Main Table for total views count. So when making an entry in View table, update main table
Views column.

Reload Avoiding

From this method, check that page is Reloaded or Refreshed. If not So, update main table column Views.
A simple php script,

function view_update($post_id) {
    $refreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
    if($refreshed == '') {
        #update query,
        $dbcon->query("update quotes set views=views+1 where quote_id=$post_id");

PHP MySQL Web optimization Posted on 2016-05-29 08:55:09