Page 1 of 1

Multipage visitor counter into one txt file

Posted: Thu Jun 29, 2006 1:38 pm
by Peuplarchie
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 ?

Posted: Thu Jun 29, 2006 1:52 pm
by mabufo
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.

Posted: Thu Jun 29, 2006 1:55 pm
by jamiel
If you are on Linux, the kernel should deal with this for you.

Posted: Thu Jun 29, 2006 1:59 pm
by Chris Corbyn
mabufo 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.
flock() helps to avoid two requests writing at the same time. I'd just do an

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);
    }
}

Posted: Thu Jun 29, 2006 2:04 pm
by Peuplarchie
Sorry I'm using window 2003 server.

Thanks for that piece of code but I dont have a clue on how to make my counter using the same flatfile to count, not more retriving it.
can someone help me

Posted: Thu Jun 29, 2006 4:49 pm
by Roja
bbclone does pretty much what you are looking to accomplish: http://bbclone.de/