Good day to you all !
I was wondering how can I make a multipage counter which all the result will go into the same txt file and would still be readable for each page to show there own result ?
Multipage visitor counter into one txt file
Moderator: General Moderators
- Peuplarchie
- Forum Contributor
- Posts: 148
- Joined: Sat Feb 04, 2006 10:49 pm
- mabufo
- Forum Commoner
- Posts: 81
- Joined: Thu Jul 10, 2003 11:11 pm
- Location: Orland Park, IL
- Contact:
I think you would have problems with more than one instance of the code trying to write, or read to the file at once - even by temporarily locking the file, you could still potentially have a problem with the user having to wait for the file to be unlocked.
From what I hear, it would be better to not use a flat file, and instead use a relational database - such as mySQL. mySQL has built in procedures for instances where more than one thing tries to perform the same operation.
From what I hear, it would be better to not use a flat file, and instead use a relational database - such as mySQL. mySQL has built in procedures for instances where more than one thing tries to perform the same operation.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
flock() helps to avoid two requests writing at the same time. I'd just do anmabufo wrote:I think you would have problems with more than one instance of the code trying to write, or read to the file at once - even by temporarily locking the file, you could still potentially have a problem with the user having to wait for the file to be unlocked.
From what I hear, it would be better to not use a flat file.
Code: Select all
if (is_writable($file)) //First check
{
$handle = @fopen($file, 'w');
if ($handle) //Second check (something may have locked it in those microseconds)
{
if (@flock($handle, LOCK_EX)) //w00t we now can trust that we have exclusive access
{
//Go ahead and write
fwrite($handle, $foo);
fclose($handle);
}
else fclose($handle);
}
}- Peuplarchie
- Forum Contributor
- Posts: 148
- Joined: Sat Feb 04, 2006 10:49 pm
bbclone does pretty much what you are looking to accomplish: http://bbclone.de/