Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7402077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:38:59+00:00 2026-05-29T04:38:59+00:00

I am trying to make a unique visitors counter for my pages using mysql

  • 0

I am trying to make a unique visitors counter for my pages using mysql and php. Im my DB table i have a “views” column and a “last_ip” column.

If the ip of the current user is equal to the last ip stored on DB the counter dies, if the current user ip is different from the last ip stored on DB the current user ip is stored as last ip on DB and the counter sums +1 to the views on DB.

The main idea is:

1 – check the ip of the current user and save it to variable $viewer_ip

2 – check the last ip stored on DB and save it to variable $last_viewer_ip

3 – compare those 2 variables, if $viewer_ip =! $last_viewer_ip the function should store $last_viewer_ip in “last_ip” field and sums +1 in “views” field. Else it should do nothing.

<?php
        $viewer_ip = $user_ip = $h->cage->server->testIp('REMOTE_ADDR');

        $sql = "SELECT post_last_viewer_ip FROM " . TABLE_POSTS . " WHERE post_id = %d";
                $last_viewer_ip = $h->db->get_var($h->db->prepare($sql, $h->post->id));

        if ($viewer_ip AND $viewer_ip != $last_viewer_ip) {     
                $sql = "UPDATE " . TABLE_POSTS . " SET post_last_viewer_ip = '" . $viewer_ip . "' WHERE post_id = %d";
                $h->db->query($h->db->prepare($sql, $h->post->id));
        }

        if ($viewer_ip != $last_viewer_ip) {
            $sql = "UPDATE " . TABLE_POSTS . " SET post_views = post_views + 1 WHERE post_id = %d";
            $h->db->query($h->db->prepare($sql, $h->post->id));
        }
?>

that code works in parts, cause it sums like 3 views, on each visit, as u can see, that code is a trash, cause i did it myself and i am no expert.

Anyone can try a fix on this? ty.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:39:00+00:00Added an answer on May 29, 2026 at 4:39 am

    You need to store the session id of your visitors to the database to be able to do that kind of thing correctly. A unique visitor is considered one that is there for one specific session. If the user closes the browser and comes back, then it is another visitor.

    If you want truly unique visitors, you need to store cookies too and use them to identify your visitors, but then again, cookie blocked? Cookie flushed? You’re screwed…

    Last method is to force a login, with a login you usually have a user_id, that user_id becomes your unicity.

    I’ll let you decide how you want to handle your unicity…

    For the storage part, you need at least 1 table where you store the requests and identity of your requesters. Store in that table the following information:

    1. Page/SpecificRequest
    2. UserIP/SessionID/CookieID/UserId
    3. RequestDate
    4. RequestTime

    Then on each page request, store a request in that table such as:

    INSERT INTO myrequests VALUES(
        $_REQUEST['URI'],
        session_id(),
        date('Y-m-d'),
        date('G:i:s')
    )
    

    And then, to retrieve the unique visitor count, you just group on the data:

    SELECT RequestDate, COUNT(*) AS uniquevisitors 
        FROM myrequests 
        GROUP BY RequestDate, session_id()
    

    Good luck

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a mysql table and have got stuck with making
I have a unique problem/situation here. Trying to make it as simple as possible.
I'm trying to make a control template for a table where each column is
I'm trying to group (or at least make unique) a column m_group_name unless the
So im trying to make unique profile views, i thought of creating 2 tables
I am trying to make 3 table tags system. I have 3 table in
I am trying to make update statement in MySql table - just migrating data
I'm currently trying to make a self referencing table in MySQL, however it seems
Trying to make a MySQL-based application support MS SQL, I ran into the following
I'm trying to make a vote/poll application in Flash using Actionscript 3. Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.