Multipage visitor counter into one txt file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Multipage visitor counter into one txt file

Post 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 ?
User avatar
mabufo
Forum Commoner
Posts: 81
Joined: Thu Jul 10, 2003 11:11 pm
Location: Orland Park, IL
Contact:

Post 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.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

If you are on Linux, the kernel should deal with this for you.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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);
    }
}
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Post 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
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

bbclone does pretty much what you are looking to accomplish: http://bbclone.de/
Post Reply