#!/usr/local/bin/perl # 設定部分 # ↓(あなたが設定)カウンタファイル名 $countfile = "./applstat.cnt"; # ↓(あなたが設定)HTMLファイル名(この中に特殊タグを含めておく) $htmlfile = "./jsas_main.html"; # ↓(あなたが設定)0.gif 〜 9.gif が入っているディレクトリ名。 $gifdir = './images'; ########## # ↓アクセスカウンタの内容に置換される特殊タグ $countertag = ''; ########## # カウンタファイルの形式 # 1行目:これまでの累積カウント数 # 2行目:昨日のカウント数 # 3行目:今日のカウント数 # 4行目:最終カウント年月日 ########## # ロック関連 $uselock = 1; # ロックを使うなら1。うまく動かないとき0にする。 # HTTPヘッダ print "Content-type: text/html\n"; print "\n"; # データファイルのオープン if (!open(OUT, "+<$countfile")) { $msg = '[not found]'; &show_page; exit(0); } # ファイルのロック if (!&lock_file(OUT)) { $msg = '[lock error]'; &show_page; exit(0); } # ロックをかけることができたのでファイルを更新 # カウンタ値の取得 seek(OUT, 0, 0); $totalcount = 1 + ; $yesterdaycount = 0 + ; $todaycount = 1 + ; $lastday = ; ( $sec, $min, $hour, $day, $mon, $year ) = localtime(time); $mon++; $year += 1900; $today = "$year-$mon-$day\n"; # 今日はじめてのアクセスか? if ($today ne $lastday) { ( $sec2, $min2, $hour2, $day2, $mon2, $year2 ) = localtime(time - 24*60*60); $mon2++; $year2 += 1900; $yesterday = "$year2-$mon2-$day2\n"; if ($yesterday ne $lastday) { # 昨日はアクセスしなかった $yesterdaycount = 0; } else { $yesterdaycount = $todaycount - 1; } $todaycount = 1; $lastday = $today; } seek(OUT, 0, 0); print OUT "$totalcount\n"; print OUT "$yesterdaycount\n"; print OUT "$todaycount\n"; print OUT "$lastday"; &unlock_file(OUT); close(OUT); # 置換メッセージの構築 $msg = ""; $msg .= &get_img($totalcount); &show_page; # 終了 exit(0); ########## sub show_page { if (!open(HTML, "$htmlfile")) { print "

$htmlfileが見つかりません。

\n"; exit(0); } @html = ; close(HTML); for ($i = 0; $i < @html; $i++) { if ($html[$i] =~ /$countertag/i) { $html[$i] =~ s/$countertag/$msg/gi; } print $html[$i]; } } # カウンタ値を タグに変換 # &get_img(カウンタ値); sub get_img { local($count) = @_; local($img) = ''; # 数字列を配列化 @numberarray = split(//, $count); # 一文字ごとに処理 foreach $number (@numberarray) { $img .= "\"$number\""; } return $img; } # ロック sub lock_file { local(*FILE) = @_; if ($uselock) { eval("flock(FILE, 2)"); # 2=LOCK_EX if ($@) { # flock が使えない場合、ここに来る。 return 0; } } return 1; } # アンロック sub unlock_file { local(*FILE) = @_; if ($uselock) { eval("flock(FILE, 8)"); # 8=LOCK_UN } }