2019-02-10 Pujan Niroula 1 Minute(s) Read

Create Simple Pageview Counter in PHP

img-thumb pageview counter php.jpg

Some website owner wants to display total pageviews on their web so far. This gives visitor an impression, a psychological impression. If you also want to display total pageviews on your website then this post is for you man :)

In this tutorial I will use just a file (pageview.txt) to store total pageview. I used some PHP's inbuilt functions such as file_get_contents() and file_put_contents().

Simple Pageview Counter with PHP - Source Code

<?php
class pageView
{
    //Declear file to store total pageview count
    private $file = 'pageview.txt';

    function __construct(){
        if(!file_exists($this->file)) {
            file_put_contents($this->file, '0');
        }
    }
    public function add_view() {
        $prev_count = file_get_contents($this->file);
        file_put_contents($this->file, $prev_count+1);
    }
    public function get_view() {
        $count = file_get_contents($this->file);
        return $count;
    }
}
$view = new pageView;

//add one another pageview
$view->add_view();

//Echo total pageviews
echo 'Total Pageview: '.$view->get_view();
?>

First of all it will check the file decleared on $file variable this is done with PHP's __construct function. When you call add_view function will add one more number to existing pageview. If you call get_view function this will return total pageview.

Cheers!!! Congratulation mate, you just learned to create simple hit counter in PHP. Hope to see you in comment section below...

Comments (6) Add New Comment
business2024-06-17 11:39 AM

Hey! Do you know if they make any plugins to assist with Search Engine Optimization? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good gains. If you know of any please share. Kudos! I saw similar text here: blogexpander.com

Reply
diplomaxose2024-06-14 8:42 AM

?????????? ??? ?????? ???????????? (?????) ?? ?????????? ?????? ?????? ??????? ??????? ? ????? ???????. ?????? ??????: ?????? ??????? ??? ?????? ?? ??????. ????? ??????, ?? ???????????? ????????, ????? ?? ?????? ??????????? ??????? ??????????? ? ?????????? ???. ? ????? ???????? ????? 100 ?????????? ??????????? ???????????? ?????????? ?????? ? ???????. ? ??????????? ?? ?????? ??????? ????????? ??? ??? ??????????? ????????? ? ?????? ?????????? ?????????????. - interestbook.ru

Reply
Ismaeleromo2024-06-09 3:45 PM

Keep on writing, great job! sp12.ru/com/pristroy/open/59?  diaocbinhduong.org/2018/phuc-dat-connect-214901pr.html?  nbcenter.ge/index.php?subaction=userinfo&user=ekepos?  nfrap.ru/photogallery/964/index.html?  community.wongcw.com/blogs/676196/%D0%9E%D1%81%D0%BE%D0%B1%D0%B5%D0%BD%D0%BD%D0%BE%D1%81%D1%82%D0%B8-%D0%B8-%D0%9D%D1%8E%D0%B0%D0%BD%D1%81%D1%8B-%D0%9F%D0%BE%D0%BB%D1%83%D1%87%D0%B5%D0%BD%D0%B8%D1%8F-%D0%90%D1%82%D1%82%D0%B5%D1%81%D1%82%D0%B0%D1%82%D0%B0-%D0%B7%D0%B0-11-%D0%9A%D0%BB%D0%B0%D1%81%D1%81-%D0%B2-2024?lang=tr_tr? 

Reply
Dichaeltat2024-06-09 6:19 AM

Hi there colleagues, how is all, and what you want to say regarding this piece of writing, in my view its really awesome in favor of me. stlouisbluesclub.com/read-blog/327_why-is-the-popularity-of-universities-decreasing-nowadays.html?mode=day?  mafia-game.ru/forum/member.php?u=28444?  old.kakdelat.ru/company/personal/user/1695/blog/8780/?  1001cruise.ru/carnival-cruises/114-news/762-liner-royal-princess-will-be-released-in-the-first-flight.html?  medik-look.ru/kupit-diplom-onlayn-udobno-i-byistro? 

Reply
best escape rooms2024-06-09 1:54 AM

Good day! Do you know if they make any plugins to assist with Search Engine Optimization? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good success. If you know of any please share. Kudos! I saw similar text here: Best escape rooms

Reply
Ismaeleromo2024-06-08 8:04 PM

Hi there to all, how is all, I think every one is getting more from this website, and your views are pleasant for new people. t99.vn/tap-doan-t99-va-vng-cloud-ky-ket-hop-tac-chien-luoc-toan-dien-ve-tai-chinh-cong-nghe?  topbestessays.com/?  raisenex.com/blogs/6172/Where-can-I-order-a-diploma-or-certificate-at-a?  1-carat.ru/?  zurbazar.ru/auto/ssangyong/?auto/ssangyong&Gorod=7/index.html? 

Reply