Tracking Visitors

Posted on 2006-05-31
Medium Priority
Last Modified: 2012-08-13
I am trying to write a tracker script in PHP for my site that tracks certain details about each user and logs them in a MySQL table called visitors.

What details am I able to identify about my site's visitors? I am particularly interested in the following:

These I know how to identify:

These I don't know how to identify:
screen resolution
operating system

Is it possible to identify those details using PHP?
Question by:jfredrickson
  • 3
  • 2
  • 2

Accepted Solution

soapergem earned 2000 total points
ID: 16803691
Technically, all of those details *can* be identified, but some take more work than others. The easy ones:

date/time - use the time() function.
ip - use the $_SERVER['REMOTE_HOST'] variable.
referrer - use the $_SERVER['HTTP_REFERER'] variable.

location - there is no built-in way to PHP to get a geographical location, however, there are services (probably *not* free) that will give you the geographical location of an IP address

browser - there are several ways to do this. There are a number of free scripts for PHP browser detection, I've included some links below this paragraph. Basically, all these free scripts just examine the variable $_SERVER['HTTP_USER_AGENT'] and parse it for data; it contains the info about what browser and operating system are being used. Also, take a look at the built-in get_browser() function.

screen resolution - this is, by every definition, a piece of client-side information. PHP is a wholly server-side language, so there is absolutely no way to *directly* retrieve this data using any PHP function, whatsoever. However, there is an *indirect* way to do it (although, you might not want to; I'll explain). You could use JavaScript to automatically retrieve this data (JavaScript is a client-side language), and then pass this data onto PHP using AJAX (a method to send information without actually loading a new page) all from the backend. However, depending on how often you do this, this could become very costly on your bandwidth and might slow down the rate at which pages load.

operating system - see above; the script I linked to for browser detection also handles OS detection.

Expert Comment

ID: 16803697
For location, see this link:

You'd have to pay for that service, I believe, and something went terribly wrong with their marketing department (watch their cheesy demo cartoon if you want to see what I mean), but it seems like a really solid system if you're willing to fork the money for it. There are other options, too. I imagine you could purchase a database that contains IP / Location information.

Author Comment

ID: 16803850
Thanks for the quick response Soapergem. I took a look at that browser detection script and it looks great. I am not the biggest PHP pro though, and I couldn't figure out exactly how to implement. Do you know if there are instructions anywhere? I couldn't find any. Is it just a basic function that returns certain values when the function is called? If so, how do I assign these values to variables on the page that called the function?

The GeoBytes thing looks awesome, but I would prefer to code something on my own rather than use a third-party solution. I figure if they are able to do it I can figure out how to do it too. I'll keep looking around on Google to see if I can't find some examples of determining location from IP. Maybe somebody here know some good places to start...
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Expert Comment

ID: 16804098
For the Browser/OS detection, consider using this one, actually:

You'd just save the source code there as something like "browser.php", and then use it like this:

    $br = new Browser;
    echo "$br->Platform, $br->Name version $br->Version";

As for the location thing, all that GeoBytes does (or any other system, for that matter) is maintains a (large) database of IP addresses (or address ranges) and where they come from. This information needs to be updated all the time. GeoBytes in particular just offered an easy way to integrate it into PHP, so that's why I pointed it out. But when it comes down to it, you'd be paying for keeping that database up to date. You can simply purchase databases full of IP ranges and where they come from if you like (see the link below). That's pretty much the only way to do it (keeping a list of IP addresses and where they come from) since there are no rules about which numbers should come from where. But then again, maybe some other experts know of a place to find this information for cheap, or for free, so you could leave this question open in case anyone thinks of anything. (I don't know of anything that doesn't charge, though.)

Expert Comment

ID: 16808243
Hi jfredrickson,

I believe you've gotten the answers to most of your questions:

browser                   - PHP parse from $_SERVER['HTTP_USER_AGENT']
screen resolution      - javascript: var resolution = screen.width+"x"+screen.height; var colours = screen.colorDepth;
operating system      - PHP parse from $_SERVER['HTTP_USER_AGENT']

location ?

First of all, mapping IP's to a location is a bit of a voodoo science. It's far from exact since ISP don't provide any geographical information about IP's they assign. Research about this has been ongoing since the 1990's but no real success has been made (some still attempt to correlate for example, time delays between IP nodes and distance --to gain geographical insight)

No real success has been made but the best solution is definetly held by Quova (http://www.quova.com/) -- their propriatery techniques are obscure but somehow work well (with a certain level of accurracy) and come at a price.

Good news though! There is a very good (free) version of IP to country database distributed by Maxmind:


You should find all the information you need there to start turning visitor IP's into their country name.


Expert Comment

ID: 16808303
By the away,

if you want to do it ''on your own", the first place to start is:

For an IP, do a WHOIS query and parse the responce to get the ISP's address information.
You first bet is if IP XXX.XXX.XXX.XXX has an ISP located in the US, chances are the guy with IP XXX.XXX.XXX.XXX is in the US as well.

Then you can drill down further....

Analyse the hostnames, some ISP's include state information in their hosts....

Then you can drill dwn further....

here's where creativity comes in, best of luck :)


Author Comment

ID: 16818273
Thanks for all the help. I managed to find a free IP to location service at hostip.info. They allow you to query their website for IP locations, and so I wrote a script that could parse the text from the query into variables that I can store in my database. Their service doesn't seem to be accurate, but the price is right for now. Perhaps I will upgrade to a more reliable solution in the future.

I talked with Quova, and they said that their average customer is paying approximately $2000 a month, so I think that will be a bit out of my range for the foreseeable future, but Geoselect seems like a good alternative once I am ready to upgrade.

Anyway, I think I can handle all the rest of these issues. I am now going to write a script that can parse the $_SERVER['HTTP_USER_AGENT'] variable so that I can pull out browser and OS statistics. Then I just have to figure out an AJAX solution for determining a users screen resolution and I will be all set.

Thanks for all the useful help here.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question