• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

Unique page visits

I have a website with several pages.
My client would like to see a hit counter for each page.
I'd like to do this with a session.
I have a table with IP, URL, and timestamp fields.
Here is the code that is included on each PHP page of the site:

  include '../dbConn.php';
    $host = $_SERVER['HTTP_HOST'];
    $script = $_SERVER['SCRIPT_NAME'];
    $currentURL = $host . $script;
    //echo "Current page is: " . $currentURL;
    if(((!isset($_SESSION['lastPage'])) or ($_SESSION['lastPage'] <> $currentURL)) and (!isset($_SESSION['userIP']))) {
        $userIP = $_SERVER['REMOTE_ADDR'];
        $_SESSION['userIP'] = $userIP;
        //echo "You're IP address is: " . $userIP;
        $vDate = date("Y-m-d");
        $vTime = date("H:i:s");
        $mSQL = "INSERT INTO `hitcount` (`URL`,`IP`) VALUES ('$currentURL', '$userIP')";
    $mSQL = "SELECT COUNT(*) AS `visits` FROM `hitcount` WHERE `URL` = '$currentURL'";
    while($row = $result->fetch_array()) {
        $cntValue = str_pad($row['visits'], 10, "0", STR_PAD_LEFT);
    $_SESSION['lastPage'] = $currentURL;

Open in new window

It's kinda working...
When you visit the home page the counter increments and shows the newest value.
However, when you visit subsequent pages, the count DOES NOT increment.
I want to exclude multiple visits to the sme page in the same session so I can track "unique" visits to each page and avoid F5 artificially inflating the results.
  • 2
1 Solution
I would make your session id and page name a combined unique key in the db then you can use ON DUPLICATE KEY UPDATE
To create the key
CREATE UNIQUE INDEX nameofindex ON tablename ('url','ip');

This way there will only ever be one entry per session per page, even if they go to another page and come back to that page
Then you can forget checking what page they were on and just do the insert every page regardless but still checking the current session page value so you don't do unnecessary inserts.
Ray PaseurCommented:
The professional solution is Google Analytics.  You can supplement that with your own hit counter, but that's kind of a 1998 idea.  You won't see any professionally built web site with a hit counter today.  The session may not be a useful part of the answer because it is lost after any period of inactivity.  I wouldn't worry about people hitting the F5 key -- that's an edge case.

You might think about adding the count to the data base table.  If you keep one record for each visitor, you'll have a very long table, indeed!

Looks like it does not display $cntValue anywhere.
dimmergeekAuthor Commented:
Thanks! So much simpler than what I had.
dimmergeekAuthor Commented:
Ray, thank you for your post.
The client is requesting the counter....I tried to steer him away.
This code is counter.php which is an include on subsequent pages.
$cntValue is used by the parent page.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now