Reputation:
I am trying to write a script or more like come up with a simple logic to track clicks or visits. I don't need to track each page, just as long as they land on the homepage is where I want to store it as 1 click.
First of all, is it safe to say that tracking by IP is far from accurate because many users can be under the same IP?
Currently my logic to do this is set a cookie on client side with a flag when they land on the homepage for the first time. At that point, I would update the database with 1 unqiue click as unique. Then each time this same visitor visits, the homepage would check for the flag and if it exists, update the database with 1 raw click....etc.
I do know that if they dump their cookies, it would throw off the data but generally, is this how it is done?
Do you have a better way?
Upvotes: 3
Views: 2831
Reputation: 801
Try this to retrieve the ip of the visitor, it works fine for my statistics :
function get_ip()
{
if($_SERVER){
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$adress = $_SERVER['HTTP_X_FORWARDED_FOR'];
elseif(isset($_SERVER['HTTP_CLIENT_IP']))
$adress = $_SERVER['HTTP_CLIENT_IP'];
else
$adress = $_SERVER['REMOTE_ADDR'];
} else {
if(getenv('HTTP_X_FORWARDED_FOR'))
$adress = getenv('HTTP_X_FORWARDED_FOR');
elseif(getenv('HTTP_CLIENT_IP'))
$adress = getenv('HTTP_CLIENT_IP');
else
$adress = getenv('REMOTE_ADDR');
}
return $adress;
}
Upvotes: 3