PHP - Flat file or database

Associate
Joined
27 Jun 2006
Posts
1,473
Evening all, just a general opinion needed here.

I have a website that people visit, now I want to make sure that they can only access once a day.

What I plan to do is write their IP address to either a flat file or MySQL database so if they come back, the IP is checked to see if its in the database / file already - if it is they get redirected to a 'Try again tomorrow' page else they can access the page.

At a guess the page is visited by between 300 - 700 people so my question would a flat file or database be better to implement?

The database would involve accessing / searching / inserting data whereas the text file would be write toand read into an array.

I am leaning towards the flat file just because I am lazy but wondered what you guys would do.

Cheers
 
I'd use MySQL - better performance and you can use standard SQL, rather than writing your own functions to handle searching and updating a file. There is also better redundancy with a MySQL database.
 
Would there be much of a performance difference if the database is one column with IP addresses and the text file just contains IP addresses?

Not questioning your answer, just wondered how much difference there would be :)

Thanks for the reply so quickly as well
 
I log referrers with MySQL on a site which gets 9000-20,000 unique users per day and anything from 40,000-100,000 page impressions per day (this allows me to monitor what other sites are saying about us/linking to better than analytics software does). It works OK, however, there is quite a bit of strain placed on the server when doing so. I've been fiddling with sqlite, which can do the same thing, but appears to put the server under far less load. This would be worth considering if you run a site that might suffer from the Digg/Slashdot/Stumbleupon effect.
 
Would personally go down the database route, especially if your getting on average 500ish hits/day. There's a news script called cutenews that uses flat files to store everything in - there's a huge article regarding flat files vs databases on theor forums but for the life of me cant find the link.
 
Evening all, just a general opinion needed here.

I have a website that people visit, now I want to make sure that they can only access once a day.

What I plan to do is write their IP address to either a flat file or MySQL database so if they come back, the IP is checked to see if its in the database / file already - if it is they get redirected to a 'Try again tomorrow' page else they can access the page.

At a guess the page is visited by between 300 - 700 people so my question would a flat file or database be better to implement?

The database would involve accessing / searching / inserting data whereas the text file would be write toand read into an array.

I am leaning towards the flat file just because I am lazy but wondered what you guys would do.

Cheers

I take it you will be cleaning the database daily? Or will you be sending a time/date stamp to the database as well?
 
For a simple once a day check with low usage, you could do it very easily with multiple files in a directory e.g.

Code:
# Hash the IP to a string value
# Check if a filename with the hash value exists on filesytem
## If file exists & last-modified < 86400s, prevent user
## If file exists & last-modified > 86400s, touch file and allow user
## If file doesn't exist, touch file and allow user

Run a cron-job to remove all files not recently modified every x days. Simple filesystem access like this should be very fast, is automatically cached and a lot less memory hungry than running a DB instance.
 
I'm gonna agree with Augmented.

Unless you're already running a database, in which case you could integrate it with what's there already. Or if you wanted to benefit from some nice logging features further down the line.

If you ever need to evolve the IP logging any further than logging 'last accessed' times, then go down the database route. Don't start creating some half database half file-based system - things will get messy.

I had to deal with a mess like that on a Sony website, couldn't believe such a big site had such a mess of code. It made maintenance and adding features such an issue. When they asked us to implement some content management, to do things sensibly it all had to be moved over to databases anyway. So in the long run, it would've been cheaper & easier to maintain if it were database'd as soon as the site started getting complicated - rather than bodged with more & more file-based mess.
 
I agree that the flat file version is preferable over MySQL in terms of performance. I'm currently using the MySQL/cron version, but this does place lots of load on the server, and that causes issues when we have heavy traffic. The sqlite option is lighter on the server and has the benefits MySQL provides, while being much easier to code and manipulate than the text file version.

Edit: sqlite uses a flat file database that can be queried with SQL.
 
Last edited:
Cheers for all the suggestions - in the end I did 2 versions, 1 flat file and 1 database solution.

Will put them through their paces on some test sites to see how they do :)

Thanks again!
 
Back
Top Bottom