Page 1 of 1

Preventing multiple posts using a Flat File Database

Posted: Sun Feb 13, 2005 5:09 pm
by section31
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

Posted: Mon Feb 14, 2005 1:58 pm
by Buddha443556
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.

Posted: Mon Feb 14, 2005 2:05 pm
by section31
But going back to my question. What happens when 2 people access the same file when you use an excluseive lock? Will it fail?
However, flock()'ing after fopen() is useless but has been (somewhat) fixed.
What does that supposed to mean...wh is it useless...They do it on php's manual.

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.

Posted: Mon Feb 14, 2005 2:07 pm
by feyd
the lock should fail, if the fopen doesn't fail first, unless the OS/PHP version doesn't support locking correctly

Posted: Mon Feb 14, 2005 2:10 pm
by section31
FAILS 8O 8O . Well that seems to be somewhat primitive if you ask me. You think php would handle it better by waiting or soemthing.

So, after I implement flock into this script....can you rate the code on efficiency? Criticism is welcome.

Thanks guys.