in reply to On server, counter file is read-only

If you can read, but not write, the file, it may be a matter of permissions. Make sure that the web server has permission to write to the file. Also, make sure the file path in your script is still correct after the upload.

Update: A few suggestions:

#!/usr/bin/perl use strict ; # ALWAYS ALWAYS ALWAYS use use warnings ; # strict and warnings! Always! use CGI qw/ :standard / ; # Rather than printing the HTML. my $number_of_digits = 6; open( COUNTING, #"< D:/StronyKlientow/cematsil.bptnet.pl/main/Alicja/perl/counte +r.txt" "< ./counter.txt" # For my testing ) or die "can not open file: $!" ; my $count = <COUNTING>; chomp $count ; # You pretty much always want chomp instead of chop. close (COUNTING); $count++ ; open( COUNTING, # ">D:/StronyKlientow/cematsil.bptnet.pl/main/Alicja/perl/counte +r.txt" ">./counter.txt" # For my testing ) or die "Cannot open: $!\n"; print COUNTING $count ; close COUNTING ; my @digits = split( //, $count ) ; my $spline = '%0' . $number_of_digits . 'd' ; $count = sprintf( "$spline", $count ) ; my $image = '' ; my @digitcount = split( //, $count ) ; foreach my $digitimage (@digitcount) { $image .= img( { 'src' => "../obrazki/$digitimage.gif", 'vspace' => 0 } ) ; } print header, start_html( { 'bgcolor' => 'lightblue' } ) ; print table( { 'align' => 'center', 'border' => 0 }, Tr( td( { 'align' => 'center', 'valign' => 'top' }, $image ) ) ) ; print end_html ; exit;

Update 2: Please read Abigail-II's comments below. [s]?he is, of course, absolutely right; the code I posted does nothing to address the case where two people load your page at the same time.

I've only ever actually made one web counter, a couple years ago in Python, so I didn't really think about all the issues. However: I still say, use strict, use warnings, and take advantage of CGI.

YAU: And yes, merlyn has a WebTechniques column on this.


_______________
D a m n D i r t y A p e
Home Node | Email

Replies are listed 'Best First'.
Re: counter
by Abigail-II (Bishop) on Jul 19, 2002 at 10:35 UTC
    That is a horrible, horrible suggestion. It will work fine as long as you get only a few hits a day. But as soon as you get two hits almost simultaneously, you will have problems. If you are lucky, your counter will display a bogus number. If you are unlucky, your visitors will get a broken image icon as the number.

    This is a Unix environment. That means, multitasking. It's not a PC, two things can actually happen at the same time! Hence, you need to use critical sections in your code - and lock the file. Here's a suggestion of how to tackle the problem:

    use strict; use warnings 'all'; use Fcntl qw /:DEFAULT :flock :seek/; my $counter_file = "/tmp/counter"; sysopen my $fh => $counter_file, O_RDWR | O_CREAT, 0644 or die "Failed to open $counter_file: $!\n"; flock $fh => LOCK_EX or die "Failed to flock $counter_file: $!\n" +; chomp (my $counter = <$fh> || 0); $counter ++; seek $fh => 0, SEEK_SET; print $fh "$counter\n" or die "Failed to write to $counter_file: $! +\n"; truncate $fh => tell $fh or die "Failed to truncate $counter_file: $! +\n"; close $fh or die "Failed to close $counter_file: $!\n" +; ... Do whatever you want with $counter here ....
    But this is just shown for educational purposes. Using counters on webpages is so mid-nineties, people have done it before, and there are ready to use programs and modules out there. And I bet Randal has a column about it....

    Abigail

Re: Re: counter
by Anonymous Monk on Jul 19, 2002 at 07:04 UTC
    It is the same path like for reading - ISN'T IT ??? How can I "make sure that the web server has permission to write to the file"????

      Is the web server running on Windows or Unix/Linux? I'm not too familiar with Windows permissions, but if it's on Unix or Linux, check that the user the web server runs as (might be 'nobody', 'apache', etc.) has write permission to your file. ls -l myfile.pl will tell you what the permissions are currently set to.

      As for the path, what I meant was this: You said that on the server, it refuses to write to the file. You didn't actually say it was reading successfully. Is it?


      _______________
      D a m n D i r t y A p e
      Home Node | Email