#!/usr/bin/perl
#use strict;
use warnings;
# 同一IPアドレスの場合にカウントしない時間間隔(秒)
#$interval = 600;
$interval = 1;
# ログ・集計用ファイルを置くフォルダ(最後は/で閉じること)
#$logdir = './LOG/';
$logdir = './';
# カウントログのファイル名(特に変更の必要なし)
$loga = 'count.txt';
$logb = 'count.bak';
# 数字を3桁ずつコンマで区切るか(区切る:1 区切らない:0)
$comma_use = 0;
# 集計を行うか(行う:1 行わない:0) オプション
$calcuse = 1;
# 集計用ファイルの名前(集計する場合のみ) オプション
$dayfile = 'day.txt'; # 今月の日計
$dayback = 'dab.txt'; # 先月の日計
$monfile = 'mon.txt'; # 月計
# 世界時からの時差設定(秒)(特に変更の必要なし)
$ajst = 9*60*60;
&main_routine; # この行は消さないで
# JavaScript部分
# ここで色、文字、タグ等を直接修正してください
print <<_EOM_;
document.write('累計 0', "$total_cnt",'');
document.write(' 本日 0', "$today_cnt",'');
document.write(' 昨日 0', "$yester_cnt",'');
//End-->
_EOM_
# 設定ここまで(ここから先は腕に自信のある方のみ弄ってください)
exit;
# -------------
# Main Routine
# -------------
sub main_routine
{
$loga = $logdir.$loga;
$logb = $logdir.$logb;
$new_id = "$ENV{'REMOTE_ADDR'}\/$ENV{'HTTP_X_FORWARDED_FOR'}\/$ENV{'HTTP_USER_AGENT'}";
$seconds = time;
$mday = (gmtime($seconds + $ajst))[3];
$oka=0;
$okb=0;
@linea = ();
@lineb = ();
# Read Log
if (-f $loga) {
open(LOGA,"< $loga") || &error("$loga を読めません");
$linea = ;
close(LOGA);
@linea = split(/\|/, $linea);
$oka = 1 if ($linea[6] eq 'EOF');
}
## ファイルのパーミッションが666でないと書きこみができないときは
## 以下の5行の行頭の # を取り外してください
else {
open(LOGA,"> $loga") || &error("$loga を作れません");
close(LOGA);
chmod(0666, $loga);
}
if (-f $logb) {
open(LOGB,"< $logb") || &error("$logb を読めません");
$lineb = ;
close(LOGB);
@lineb = split(/\|/, $lineb);
$okb = 1 if ($lineb[6] eq 'EOF');
}
## ファイルのパーミッションが666でないと書きこみができないときは
## 以下の5行の行頭の # を取り外してください
else {
open(LOGB,"> $logb") || &error("$logb を作れません");
close(LOGB);
chmod(0666, $logb);
}
$write = ($linea[0] > $lineb[0]) ? 'b' : 'a';
$write = 'b' if (!$okb);
$write = 'a' if (!$oka);
if ($write eq 'b') {
($total_cnt,$today_cnt,$yester_cnt,$old_date,$old_tm,$old_id) = @linea;
$log = $logb;
} else {
($total_cnt,$today_cnt,$yester_cnt,$old_date,$old_tm,$old_id) = @lineb;
$log = $loga;
}
# Write Log
if (($seconds - $old_tm) > $interval || $new_id ne $old_id) {
$total_cnt++;
if ($mday == $old_date) { $c_flag = 0; $today_cnt++; }
else { $c_flag = 1; $yester_cnt = $today_cnt; $today_cnt = 1; }
&day_calc if ($calcuse && $c_flag);
$line = "$total_cnt\|$today_cnt\|$yester_cnt\|$mday\|$seconds\|$new_id\|EOF\|";
open(TMP,"+< $log") || open(TMP,"> $log") || &error("$log に書きこめません");
eval 'flock(TMP, 2);';
truncate(TMP, 0);
seek(TMP, 0, 0);
print TMP $line;
close(TMP);
}
if ($comma_use) {
$total_cnt = &fig_place($total_cnt);
$today_cnt = &fig_place($today_cnt);
$yester_cnt = &fig_place($yester_cnt);
}
$yester_cnt = 0 if (!$yester_cnt);
&txt_header;
}
# Mark by a comma each three figures
sub fig_place
{
local($_) = $_[0];
## perl4
# 1 while s/(.*\d)(\d\d\d)/$1,$2/g;
## perl5
1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g;
return($_);
}
# Error Output
sub error
{
&txt_header;
print "document.write(\"$_[0]
\"); \n";
print "//End-->\n";
exit;
}
# Text Header
sub txt_header
{
$|=1;
print "Content-type: text/plain\n\n";
print "