#!/usr/bin/perl # # today+x: Print Today's Access Counter (works with countx.cgi) # file: todayx.cgi # # Copyright (C) 2001 KOMORIYA Takeru # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # See "http://www.paken.org:8080/aaf/download/GPL" for more details. # # Contact to author: # KOMORIYA Takeru, AAF Sendai Lab., Japan # E-mail: komoriya@paken.org # URL: http://www.paken.org:8080/ # #--------------------------------------------------------------------- # Ver 0.1 (8/21, 2001): First release. #--------------------------------------------------------------------- # Usage # 1.Save this file as "todayx.cgi" # 2.Change permission of this file "chmod 755 todayx.cgi" # Note: SSI must be work with your web server. # # To setup couter in your page: # 1.Insert this line where you want to print access counter. # #--------------------------------------------------------------------- # Directory for log-file (must be followed by '/') $logdir = "./countx_data/"; # Lock for inhibit multiple invocation # 0: Don't lock, 1: Lock(symlink), 2: Lock(mkdir) $lockkey = 1; # Number of digit $digit = 3; # Lock-file $lockfile = $logdir . ".lock"; # Counter of 'Today' $logfile = $logdir . "today.log"; #-------------------------------------------------------------- if (-e $logfile) { # Lock &lock if ($lockkey); open(IN,"$logfile") || &error; $logdate = ; $newcount = ; chomp($newcount); close(IN); # Unlock &unlock if ($lockkey); }else{ $newcount = 0; } # Decimal to String $count = sprintf("%0$digit\d",$newcount); # Output conter print "Content-type: text/html\n\n"; if ($hidden == 0){ print "$count"; } exit; #-------------------------------------------------------------- sub lock { local($retry)=5; # delete old lock (older than 3 minutes) if (-e $lockfile) { ($mtime) = (stat($lockfile))[9]; if ($mtime && $mtime < time - 180) { &unlock; } } # symlink lock if ($lockkey == 1) { while (!symlink(".", $lockfile)) { if (--$retry <= 0) { &error; } sleep(1); } # mkdir lock } elsif ($lockkey == 2) { while (!mkdir($lockfile, 0755)) { if (--$retry <= 0) { &error; } sleep(1); } } } sub unlock { if ($lockkey == 1) { unlink($lockfile); } elsif ($lockkey == 2) { rmdir($lockfile); } } sub error{ print "Content-type: text/html\n\n"; print "ERROR"; # unlock &unlock if ($lockkey); exit; }