1

I need to create a visitor counter for my websites and I'm wondering if it is better to store and read the information from a txt file located somewhere in my host or directly from the database.

Using a database would mean that a DB entry will be created for every single visitor that will access the site and honestly I don't think that would be OK.

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
TerminatorX
  • 307
  • 3
  • 13

2 Answers2

4

File counter - when just count. DB counter - when visit tracking, depenences, analysis, aggregation.

Read file is really faster, when file is small. Still, there may be a race condition effect, when site is heavy loaded. There is hard to show linked data, if needed. For this needs there is a great solution: Database Management Systems.

Database (with good design) allows to avoid race condition. Also it's a better solution for large amount of linked data structures. It's better, when you need to log visits, referers, etc...

DB Suggestions: you might store counter in one row of global_settings table and update it within each page visit, or you might get it by registrating each visit in visit table (with additional data, like IP, DateTime, UserID, etc...) with SELECT COUNT(*) from visit;.

There is another related topic here.

Community
  • 1
  • 1
BlitZ
  • 12,038
  • 3
  • 49
  • 68
0

Loading anything from text files is pretty bad practice. Using a database is the better solution. Databases are meant to store large amounts of data, so it is perfectly acceptable.

Cezary Wojcik
  • 21,745
  • 6
  • 36
  • 36
  • 3
    Loading from text files is not bad practice. I cache many larger database intensive pages into flat text. Right tool for the job – Kai Qing Apr 25 '13 at 04:01
  • Caching is a different story; I was referring to the storage of data in a general sense. – Cezary Wojcik Apr 25 '13 at 04:03
  • 1
    Check out [**this**](http://stackoverflow.com/questions/6853482/flat-file-vs-database-speed). – BlitZ Apr 25 '13 at 04:15