问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/16206131/should-i-use-a-text-file-or-a-database-for-a-php-visitor-counter