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

Leave a Comment