Counter script with PHP
From: Alfred Molon (alfred_molon_at_yahooDELETE.com)
Date: 04/26/04
- Next message: Paul Delannoy: "Re: Help including text"
- Previous message: Alfred Molon: "Image processing functions in PHP"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 26 Apr 2004 21:50:40 +0200
I've implemented a simple script to count the number of page views of
the image pages of my site. For instance the photo gallery at
http://www.molon.de/galleries/Malaysia/Sarawak/Dance/
contains a total of nine images and the counts are store in the file
http://www.molon.de/galleries/Malaysia/Sarawak/Dance/stat.txt
Every time the local img.php script is called to serve a page it
accesses the local stat.txt file and increments by one the corresponding
line.
The trouble is that every now and then the stat.txt file gets corrupted
- probably there are collisions with two imp.php scripts trying to
access stat.txt at the same time. I'm saying "probably" because I'm not
sure what causes the stat.txt file to become corrupted.
To make an example, while I'm writing this, the following file is
corrupted: http://www.molon.de/galleries/Hungary/Buda/stat.txt
(some lines are empty)
(Note: the script will autorepair this file later, by replacing empty
lines with "0".)
By the way, there are img.php scripts and stat.txt files in every photo
gallery.
I tried to solve the problem by locking the access to the stat.txt file
with the following code:
$fe = fopen ("stat.txt", "w");
if (flock ($fe, LOCK_EX)) {
fwrite ($fe, $buf);
flock ($fe, LOCK_UN);
}
// where $buf is the updated content
But so far without success. What am I doing wrong ?
-- Alfred Molon http://www.molon.de/Galleries.htm - Photos from Myanmar, Brunei, Malaysia, Thailand, Laos, Cambodia, Nepal, Egypt, Austria, Budapest and Portugal
- Next message: Paul Delannoy: "Re: Help including text"
- Previous message: Alfred Molon: "Image processing functions in PHP"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|