About hit counter for a website

I want to make a hit counter for a website. I want it not to increment the number by refreshing the page. I've tried several counters but they increase their score by refreshing the page. Any ideas...?

+3


source to share


1 answer


If you want to do this manually, the easiest way to install a counter on your site is to use a session.

First when your site link opens, start a session and register the counter variable as $ _SESSION ['count'] = 1 in php and then increment it by one.

Now use a simple if-else statement to check if the value of $ _SESSION ['count'] is 1 or greater than 1, if 1 then send a request to the server via ajax to keep that single account the first time you visit your website in your database table (means you just need to update the value of the table column in your db).

Now, if the user closes his browser, the session will be automatically destroyed, or if you have access to your login type, then simply disable this session variable before checking any condition for the registered value "$ _SESSION ['count']", whatever one request was not generated to update the number of server visits in your db.



And to distinguish between logged in user and visitor, just use another session variable and use that to determine whether the $ _SESSION ['count'] variable should be set or not. I am providing you with 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 can give you at least an idea on how to do this. Thank you, waiting .. !!! :) :)

+1


source







All Articles