views:

147

answers:

4

I want to record number of hits to the pages and I'm thinking either plain file-based or SQLite storage.

File-based option
A file includes only an integer incremented with each page visit and every page has a unique file name. If I open a file using a mode, I can write but is it possible to not to close it in order to save from opening and closing the file each time?

SQLite option
A simple table with columns PageName and Hits. Same question again is it possible to not to close it in order to save from opening and closing the db each time?

+8  A: 

Google Analytics. Unless you need to do it in-house.

Rolling your own solution in PHP can be as simple or complicated as you like. You can create a table to store ip-address (not always reliable), the page location, and a date. This will allow you to track unique hits for each day. You may want to schedule a task to reduce the amount of records to a simple row of date, numOfUnique.

Another method is parsing your log files. You could do this every 24 hours or so as well.

Jonathan Sampson
Agree, preferable use third-party javascript hit counter.. Check out Piwik - http://piwik.org/ which a excellent replacement for Google Analytics, if you don't using adwords and such.
Stojg
A: 

It's not possible in any of your cases. PHP applications are run when user requests something from them, they generate the result and then shuts down. So even if you don't close db connection or file they will be closed automatically. But I don't know why opening db connection or a file to write would be a problem?

RaYell
A: 

It's difficult to give particularly useful answers in the absence of how much traffic you're expecting (other than Jonathan Sampson's comment that you might be better off using Google Analytics).

File-based option:

I don't think it's possible to keep the file open. Also, you'll probably bump into concurrent write problems unless you employ some kind of locking mechanism.

SQLite option:

I think this is probably the way to go, if you've not already got a database open. I doubt that opening/closing the db each time will be a bottleneck - try it and profile.

Dominic Rodger
+3  A: 

If you really have to do this in-house, you should go with the sqlite method. While there's a little overhead (due to opening the database file), there can also be notable benefits in storing structured data.

You could for example add a date field, and then get daily/hourly/monthly/whatever data for each page.

You could also add the IP address for each visitor, and then extract visits data. That way you could easily extract data about your site users' behaviours.

You could also store your visitors' user-agent and OS, so you know what browsers you should target or not.

All in all, inserting that kind of data into a database is trivial. You can learn a lot of things from these data, if you take some time to study these. For that reason, databases are usually the way to go, since they're easy to manipulate.

Nicolas
Not that I think this is a bad answer, but I really wish people would wait 24 hours before selecting an answer.
Imagist