Take the 2-minute tour ×

I have a site where I want to record how many unique views an entry gets. At the moment I'm storing each view in a table (see below) but I can't help feeling this is too heavy handed.

| postid |  remote_addr  |
|   1    |  |
|   2    |    |

Obviously this requires at least one query per page, in addition to the rest of the page content, so is there a simpler/lighter way of doing this?

share|improve this question
Why is 1 query per page a problem? It's not that important unless you already have performance issues. –  Ward Werbrouck Mar 9 '09 at 18:19
@code: sounds trivial, but each query is important, maybe not so much on smaller sites, but it's good practice to follow. –  Stiropor Mar 9 '09 at 18:23
@code-is-art: True, there aren't many issues. It's just something that looks like overkill from my point of view. Might just be me =P –  Ross Mar 9 '09 at 18:28

4 Answers 4

up vote 2 down vote accepted

Is this something you can get from your web logs? If it is, you can just get a log parser and filter your pages, and unique visitors.

share|improve this answer
This does sound like an interesting solution an automated script could take care of. –  Ross Mar 9 '09 at 18:24

I strongly suggest you use Google Analytics or any other similar tool, that gives you a very good report and detailed statistics about your visitors.. unique visits, time on site, returning visitors, etc.

It's also easy to install, you just add some javascript code you get on that site.

share|improve this answer
I do use that but this is for publically displaying the view count, such as on Stack Overflow. –  Ross Mar 9 '09 at 18:23
Than afaik this is the easiest way, just use sessions and cookies to make sure you don't count someone twice. One query will be wasted, but unless you have more than 500.000 pageviews daily, it shouldn't be so horrible :) –  Stiropor Mar 9 '09 at 18:28

This is something that I've tried to do in the past, and gave up in favor of just letting my log analyzer do what it's good at. When I did do it, I used the technique you're describing. It didn't incur much overhead, but I did have to write all of my own logic for reporting and filtering bots...not fun.

I'll also point out that just storing an IP address isn't going to guarantee that you're recording uniques. It's not uncommon for your visitors to be coming from behind a proxy (especially AOL I've noticed in years past).

share|improve this answer
I'm debating limiting views to actual userids but this only counts logged in users. I think IPs are probably good enough for now. –  Ross Mar 9 '09 at 18:25

Your Answer


