Skip to content
Advertisement

How can I add Unique Visit counter for a website? [closed]

I want to make a unique visit counter for a Php website. I don’t want to increase the number by refreshing the page.
It’s not like simple visiting counter. I tried several counters but they are increasing their count by refreshing the page.
Have any ideas ..?

Advertisement

Answer

If you want to do it manually the simplest way to set counter to your website is by using session.

First when the link of your website is opened, start a session and register a counter variable as $_SESSION[‘count’] = 1 in php and then increment it by one.

Now use simple if-else statement to check whether $_SESSION[‘count’] value is 1 or greater than 1, if 1 then send a request to server via ajax just to store that single count on first visit to your website in your table of database (means you have to just update your column value of table in your db).

Now if the user closes his / her browser then session will be automatically destroyed or else if you have a login type access to your website then just unset that Session variable before checking any condition for registered value of “$_SESSION[‘count’]”, so that no any request will be generated to update visit count to server in your db.

And for distinguishing between logged in user and visitor just use another session variable and use it to determine whether to set $_SESSION[‘count’] variable or not. I am providing you a code snippet so that you can understand what I am trying to explain.

<?php
    session_start();
    //Check logged in session variable if variable not set then execute 
    // following code block
    if(isset($_SESSION['count']) {
        if($_SESSION['count'] == 1)
            //your ajax request to store visit count in db (executed only once)
        $_SESSION['count']++;
    } else {
        $_SESSION['count'] = 1;
    }
?>

I hope this snippet might give you atleast an idea of how to make it done. Thank you in anticipation..!!! :):)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement