3

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • @Rick - do you need the database to be in real-time ? – ajreal Dec 26 '10 at 02:28
  • yes, cookies is the general way to do this. – RobertPitt Dec 26 '10 at 02:38
  • @ajreal - how do you mean "real-time"? –  Dec 26 '10 at 02:45
  • @Rick - There are lots of web access log tools for this purpose, and statistics generated by these tools won't be in real-time generally. So, make your life easier to deploy one of these tools instead if you no plan for real-time report/stat – ajreal Dec 26 '10 at 02:48
  • @ajreal - I don't need anything complicated so I am trying to write this myself but as this is my first time writing this type of data, I am just reaching out for people already with experience. –  Dec 26 '10 at 02:50
  • @Rick - alright, deploying a tool will be much easier that written your own. Is up-to you, anywhere – ajreal Dec 26 '10 at 02:51
  • @Rick Many users can be from the same IP, many users can be behind a proxy with same IP you have a set of variants for this. I would recommend you to first study the access_log file generated by the webserver which contains all hits your website had, once you understands how it is generated and how the IPs are in there you can possible come up with a better approach to actually monitor your site statistics. – Prix Dec 26 '10 at 03:39
  • @Prix - thanks for that however I decided to not even log IPs as I found it easier just to rely on cookies...If they delete it, well so be it but generally it will track semi-accurate clicks. –  Dec 28 '10 at 01:30

1 Answers1

3

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;
}
ajreal
  • 46,720
  • 11
  • 89
  • 119
CrazyMax
  • 781
  • 1
  • 11
  • 24