Counter script with PHP

From: Alfred Molon (alfred_molon_at_yahooDELETE.com)
Date: 04/26/04


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


Relevant Pages

  • Dynamic Photo Album using JSP
    ... albums using Javascript, however those albums using JavaScript are not ... page which lists 10 photos each, and when you click on the photo to be ... As our pages already use JSP we don't want to add a second server side ... I am happy to start working my own script. ...
    (comp.lang.java.help)
  • Re: How do I size a photo to fit?
    ... Download the free viewer Neophyte recommended. ... Also send the script with the image you plugged in to the newsgroup. ... | a digital photo I have taken so I can use it for stationery? ... | I would like to insert a photo I took into a stationery pattern I ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress.stationery)
  • Re: newbie image load question
    ... determine the images directory relative to your script: ... image create photo default -file ... porschberg wrote: ... > class VisualRep { ...
    (comp.lang.tcl)
  • Re: Inserting Photos into Container Field
    ... > to insert an employee photo into its respective employee record. ... > "manually" insert the photo BUT I am trying to script this process so ... a calculation field: ...
    (comp.databases.filemaker)
  • Re: Windows Photo Gallery uses wrong printer
    ... automate selecting the correct printer when the print window launched.. ... My problem was that Photo Gallery would always default print to the HP ... my script, but I was glad it worked. ...
    (microsoft.public.windows.vista.music_pictures_video)