tags:

views:

43

answers:

4

Currently I have a file called "hits.php" and on any page I want to track page hits I just use <?php include("hits.php"); ?>

How can I track unique visitors only though? My hits are false since it can be refreshed by the same person and hits go up.

Here's my source:

<?php
 $hits = file_get_contents("./client/hits.txt"); 
 $hits = $hits + 1; 

 $handle = fopen("./client/hits.txt", "w"); 
 fwrite($handle, $hits); 
 fclose($handle); 

 print $hits; 

?>

I don't really know how I could do cookie checking... is there a way to check IP's? Or what can I do?

Thanks StackO.

+2  A: 

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 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 loggin 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;
  }
?>
Glenn Nelson
I don't know how to do cookie checking. How can I use cookies?
Kyle
Read my answer fully, you can also check IPs. On second thought, let me add something to it.
Glenn Nelson
Marcelo Cantos
I learned about sessions and cookies from [Practical PHP Programming](http://www.tuxradar.com/practicalphp/10/0/0). Very clear, practical and thorough. (And I'd use sessions instead of regular cookies.)
willell
@Kyle, @willell I updated it to include a possible way to doing it.
Glenn Nelson
Wow thanks a lot. That is doable! Thank you Glenn Nelson:)
Kyle
Make sure you create your MySQL database with the IP address as the unique key.
Glenn Nelson
A: 
  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
Alex JL
+1  A: 

How about google analytics if you cant. you could do a database or create another file with the IPs in it, but it could get complicated with a flat file like that.

Matt
+1  A: 

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

winwaed