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 (5) Add New Comment
dobry sklep2024-03-02 6:27 PM

Hey! I'm at work surfing around your blog from my new iphone! Just wanted to say I love reading your blog and look forward to all your posts! Keep up the fantastic work! I saw similar here: sklep internetowy and also here: dobry sklep

Reply
dobry sklep2024-03-02 6:24 PM

Hey! I'm at work surfing around your blog from my new iphone! Just wanted to say I love reading your blog and look forward to all your posts! Keep up the fantastic work! I saw similar here: sklep internetowy and also here: dobry sklep

Reply
sklep internetowy2024-03-02 6:23 PM

wonderful issues altogether, you simply received a logo new reader. What might you recommend in regards to your put up that you made some days in the past? Any sure? I saw similar here: ecommerce and also here: sklep internetowy

Reply
ecommerce2024-02-28 11:44 AM

It's perfect time to make a few plans for the future and it's time to be happy. I've read this submit and if I may I wish to counsel you some interesting things or suggestions. Maybe you could write subsequent articles referring to this article. I want to read more things about it! I saw similar here: sklep and also here: e-commerce

Reply
sklep online2024-02-23 7:15 AM

Wow, marvelous weblog format! How lengthy have you been blogging for? you made running a blog glance easy. The whole look of your web site is great, let alone the content! You can see similar: e-commerce and here dobry sklep

Reply