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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.