Tracking unique visitors only?

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;
  }
?>

  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://coveryourtracks.eff.org/

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

Tags:

Php

Counter