I made a simple little script to log the ip/time of every person that posts something...EX. Login form or something. I've used scripts like this in the past to prevent brute force attacks on my logins, but I used a mysql database which makes things so much easier. I decided to implement the same method using a flat file database just to have in case I ever need it. However, I'm a bit confused on how the flock function works. I know its needed to prevent file corruption when 2 people access the file etc. But whats the difference between a shared and an exclusive lock and what happens when 2 people access the same file when you use an excluseive lock? Will it fail?
Here is what I have so far.
Code: http://section31.us/temp/test.phps
Demo: http://section31.us/temp/test.php
Preventing multiple posts using a Flat File Database
Moderator: General Moderators
- Buddha443556
- Forum Regular
- Posts: 873
- Joined: Fri Mar 19, 2004 1:51 pm
Shared lock is for reading from a file.
Exclusive lock is for writing to a file.
You really only need to worry about exclusive lock. However, flock()'ing after fopen() is useless but has been (somewhat) fixed. fopen() in versions of PHP 4.3.2 or later supports "x" and "x+" flags. These new flags are really only useful for creating lock files though as they fail if the file already exists.
Any solution will be dependent on the file system and the operating system on which it's implemented.
Exclusive lock is for writing to a file.
You really only need to worry about exclusive lock. However, flock()'ing after fopen() is useless but has been (somewhat) fixed. fopen() in versions of PHP 4.3.2 or later supports "x" and "x+" flags. These new flags are really only useful for creating lock files though as they fail if the file already exists.
Any solution will be dependent on the file system and the operating system on which it's implemented.
But going back to my question. What happens when 2 people access the same file when you use an excluseive lock? Will it fail?
Nevertheless, I guess its best to goto th bookstore an look for a php book that talks about flat file databases...and i'll take a look.
What does that supposed to mean...wh is it useless...They do it on php's manual.However, flock()'ing after fopen() is useless but has been (somewhat) fixed.
Nevertheless, I guess its best to goto th bookstore an look for a php book that talks about flat file databases...and i'll take a look.