<ot> Counters?

Alan Jackson ajackson
Fri Jan 21 21:37:01 PST 2005


On Mon, 17 Jan 2005 09:58:42 -0500
Jerry McBride <mcbrides9 at comcast.net> wrote:

> 
> Would anyone be willing to recommend a "friendly" web page visit counter? I'm 
> looking for a remote counter service that doesn't push popups and maybe 
> sponsor links. On the other hand, a sponsor link would be agreeable if it's 
> for a good use or cause....
> 
> Any tips?
> 

put this in cgi-bin and run it via 
<!--#include virtual="/cgi-bin/webcounter.cgi" -->

#!/usr/bin/perl -Tw
use strict;

## mrv: most recent visitors (by host)

## constants:
my $MAX = 10;                   # how many visitors to keep

## system stuff
$|++;
$ENV{PATH} = "/usr/ucb:/bin:/usr/bin";

## HTML stuff
print "Content-type: text/plain\n\n";

## the main program (in eval so we can trap problems)
eval {

  ## get the CGI data
  my $uri = $ENV{DOCUMENT_URI};
  my $rhost = $ENV{REMOTE_HOST};

  ## split the URI up, so we know where the file was
  my ($dir,$file) = $uri =~ m,(.*/)(.*),s;

  ## massage the directory to get the containing dir
    ###$dir = "/home/$1/public_html/$2"; # teleport specific
    ###$dir = "/home/ajackson/www/logs/"; # website
    $dir = "/home/www/ajackson.org/logs/"; # website
    #$dir = "/home/ajackson/var/"; # local test

  ## go there
  chdir $dir or die "cannot cd to $dir: $!\n";

  ## set up mrv file
  my $mrv = "counter";          # compute untainted mrv name
  open MRV, "+>>$mrv" or die "Cannot open $mrv: $!";
  flock MRV, 2;                 # wait for exclusive lock
  ## from here to the close MRV, we have critical region
  ## be sure to minimize this time

  ## get current content
  seek MRV, 0, 0;               # rewind to beginning
  my $content = <MRV>;          # get current content
  chomp $content;

  ## massage content

  $content++;

  ## write new list, and release file
  seek MRV, 0, 0;               # rewind again
  truncate $mrv, 0;             # empty the file
  print MRV "$content";         # print the new content

  ## release file
  close MRV;

  ## now prepare the output
  print "$content";

};

## if an error, say so:
chomp $@, print "[error: $@]" if $@;


-- 
-----------------------------------------------------------------------
| Alan K. Jackson            | To see a World in a Grain of Sand      |
| alan at ajackson.org          | And a Heaven in a Wild Flower,         |
| www.ajackson.org           | Hold Infinity in the palm of your hand |
| Houston, Texas             | And Eternity in an hour. - Blake       |
-----------------------------------------------------------------------


More information about the Linux-users mailing list