Tracking unique visitors only?

后端 未结 7 1869
猫巷女王i
猫巷女王i 2020-12-03 05:49

Currently I have a file called \"hits.php\" and on any page I want to track page hits I just use

How can I track u

相关标签:
7条回答
  • 2020-12-03 05:58
    1. At a basic level, you can get the client's IP address by using the PHP $_SERVER['REMOTE_ADDR'] property
    2. Consider setting a cookie or using a session, though this can be defeated by deletion of a cookie or cookie rejection. See the PHP setcookie docs for more info.
    3. There are other methods for browser fingerprinting - check out all the different data you could conceivably use at https://panopticlick.eff.org/index.php
    0 讨论(0)
  • 2020-12-03 06:08

    You could save a timestamp to localStoage in javascript. LocalStoage isn't removed by the browser, so you should be save to check against that. I know that it isn't serverside checking, but it may be helpful anyway.

    0 讨论(0)
  • 2020-12-03 06:11

    There isn't a perfect solution, but the first two methods (IP address and/or cookies) are the most reliable, and a combination might be even better.

    Rather than reinventing the wheel I used an off the shelf solution. For commercial reasons I avoided Google Analytics (I don't want Google to know my web stats - their best interests are not mine). They're probably fine for non-commercial websites, or if you don't use Google for advertising. There are also dozens of alternatives. Eg I use Getclicky.com

    0 讨论(0)
  • 2020-12-03 06:13

    The simplest method would be cookie checking.

    A better way would be to create an SQL database and assign the IP address as the primary key. Then whenever a user visits, you would insert that IP into the database.

    1. Create a function included on all pages that checks for $_SESSION['logged'] which you can assign whatever 'flag' you want.
    2. If $_SESSION['logged'] returns 'false' then insert their IP address into the MySQL database.
    3. Set $_SESSION['logged'] to 'true' so you don't waste resources logging the IP multiple times.

    Note: When creating the MySQL table, assign the IP address' field as the key.

    <?php 
      session_start();
      if (!$_SESSION['status']) {
        $connection = mysql_connect("localhost", "user", "password");
        mysql_select_db("ip_log", $connection);
    
        $ip = $_SERVER['REMOTE_ADDR'];
        mysql_query("INSERT INTO `database`.`table` (IP) VALUES ('$ip')");
    
        mysql_close($connection);
        $_SESSION['status'] = true;
      }
    ?>
    
    0 讨论(0)
  • 2020-12-03 06:15

    currently i am using remote address and session ID for visitor.i think its valid visitor because a single user can visit no of times in a days and counter not depends on refresh its only depends on new session.

    0 讨论(0)
  • 2020-12-03 06:17

    I found the solution of very poor quality and just a quick and dirty way of doing it.

    I too was developing something similar and formulated a quick method which works without redundancy.

    I needed a counter for every time someone accessed another user's profile.

    Pseudo:

    • Create a table with viewer's name and viewee's name (daily_views table).
    • Check to see if exists the viewer's name with the viewee's name (on the same row).
    • If they do not exist, update user counter +1 (in users table).
    • Else do nothing.
    • Reset entire table values null every 24/12 hours via cron job.

    This will deny the same person accessing the same user profile to add 1 to the counter on refresh for the whole day (or 12 hours) whereas the above solution by Glenn Nelson would indeed add 1 to the counter, but deny adding to every user's counter at the same time.

    Not only this, but if you were to logoff and log back in to the website, then it would simply re-add to the counter in which some cases trolls and haxorz wannabe's will exploit this (as the session is destroyed and started again).

    Here are my sample tables:

    users 
    {
    user_id INT(8) auto increment, user_name varchar(32), user_counter INT(12)
    };
    daily_views
    {
    view_id INT(8) auto increment, viewer_name VARCHAR(32), viewee_name VARCHAR(32)
    };
    

    Here is sample code I've written:

    <?php
    session_start();
    $uname = $_SESSION['username'];
    $vieweepage = $_GET['name']; //gets the name of the persons page/profile via previous page/form
    $connect = mysql_connect("localhost","user","password") or die("Couldn't connect; check your mysql_connect() settings");
    $database = mysql_select_db("database") or die("Could not locate database!");
    
    $query = mysql_query("SELECT user_counter from users");
    $query = mysql_fetch_row($query);
    $counter = $query[0];
    $viewcheck = mysql_query("SELECT viewer_name from daily_views WHERE viewee_name='$vieweepage'");
    $viewrow = mysql_num_rows($viewcheck);
    
    $newcounter = $counter + 1;
    
    if($viewrow == 0)
    {
    $update = mysql_query("UPDATE users SET user_counter='$newcounter' WHERE user_name='$vieweepage'");
    $insert = mysql_query("INSERT into daily_views (viewer_name, viewee_name) VALUES ('$uname', '$vieweepage')");
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题