#!/usr/bin/perl ## 文字カウンター v1.6 (2001/10/13) ## CGI作成:銀白雅狼 ## PAINTER PLACE Of Rest Place Of Horse ## masarou.ginpaku@nifty.com ## http://homepage2.nifty.com/masarou/ #--- [注意事項] ------------------------------------------------# # 1. このスクリプトはフリーソフトです。このスクリプトを使用した # # いかなる損害に対して作者は一切の責任を負いません。 # # 2. 設置に関する質問はサポート掲示板にお願いいたします。 # #---------------------------------------------------------------# # ################## # # 設定ここから # # ################## # # ロックファイル $lockfile = "./count.lock"; # ログファイル名 $dfile = 'count.dat'; # 同一人物連続カウント拒否は1 $count_kyohi = '1'; # ################## # # 設定ここまで # # ################## # ## デコード処理 $mode = $ENV{'QUERY_STRING'}; $mode =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $mode =~ s/\r//g; $mode =~ s/\n//g; $mode =~ s/\t//g; # ホスト名収得 $host = $ENV{'REMOTE_HOST'}; if ($host eq '') { $host = $ENV{'REMOTE_ADDR'}; } if ($mode =~ /nocnt/) { $no_count = 1; } else { $no_count = 0; } if ($mode =~ /today/) { &tday; } elsif ($mode =~ /yest/) { &yst; } else { &main; } ## メイン処理 sub main { # ログファイル読込 &lock; open(IN,"$dfile") || &error("Open Error : $dfile"); $data = ; close(IN); ($count,$today,$yest,$hiduke,$so_host,$h_host) = split(/<>/, $data); # 総カウント数処理 unless ($no_count == "1") { if ($count_kyohi == "1") { unless ($so_host eq "$host") { $count++; } } else { $count++; } } # 書き込み処理 $data = "$count<>$today<>$yest<>$hiduke<>$host<>$h_host<>"; open(OUT,">$dfile") || &error("Write Error : $dfile"); print OUT $data; close(OUT); &unlock; # 表示処理 print "Content-Type: text/javascript\n"; print "Pragma: no-cache\n"; print "Cache-Control: no-cache\n\n"; print "document.write(\'$count\');"; } ## 本日カウント表示処理 sub tday { # ログファイル読込 &lock; open(IN,"$dfile") || &error("Open Error : $dfile"); $data = ; close(IN); ($count,$today,$yest,$hiduke,$o_host,$sh_host) = split(/<>/, $data); # 本日カウント処理 &hinithi; if ($count_kyohi == "1") { if ($sh_host eq "$host") { if ($hiduke == $mday) { $today_k = $today; $yest_k = $yest; $hiduke_k = $hiduke; } else { $hiduke_k = $mday; $yest_k = $today; $today_k = 0; } } else { if ($hiduke == $mday) { unless ($no_count == "1") { $today++; } $today_k = $today; $yest_k = $yest; $hiduke_k = $hiduke; } else { $hiduke_k = $mday; $yest_k = $today; $today_k = 1; } } } else { if ($hiduke == $mday) { unless ($no_count == "1") { $today++; } $today_k = $today; $yest_k = $yest; $hiduke_k = $hiduke; } else { $hiduke_k = $mday; $yest_k = $today; $today_k = 1; } } # 書き込み処理 $data = "$count<>$today_k<>$yest_k<>$hiduke_k<>$o_host<>$host<>"; open(OUT,">$dfile") || &error("Write Error : $dfile"); print OUT $data; close(OUT); &unlock; # 表示処理 print "Content-Type: text/javascript\n"; print "Pragma: no-cache\n"; print "Cache-Control: no-cache\n\n"; print "document.write(\'$today_k\');"; } ## 昨日カウント表示処理 sub yst { # ログファイル読込 &lock; open(IN,"$dfile") || &error("Open Error : $dfile"); $data = ; close(IN); ($count,$today,$yest,$hiduke,$o_host,$h_host) = split(/<>/, $data); &unlock; # 表示処理 print "Content-Type: text/javascript\n"; print "Pragma: no-cache\n"; print "Cache-Control: no-cache\n\n"; print "document.write(\'$yest\');"; } ## 日付処理 sub hinithi { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); if ($year > 50) {$year += 1900 ;} else{$year += 2000 ;} } ## ファイルロック処理 # ロック sub lock { local($flag) = 0; foreach (1 .. 5) { if (-e $lockfile) { sleep(1); } else { open(LOCK,">$lockfile"); close(LOCK); $flag = 1; last; } } if ($flag == 0) { &error("Lock Error"); } } # 解除 sub unlock { if (-e $lockfile) { unlink($lockfile); } } exit;