#! c:/perl/bin/perl # # sccnt2.cgi # シークレットカウンタ2(カウント) # # 2.00 : 12/15/06 : sccnt.cgiより同一IP制限機能を追加 # # http://www.hidekik.com/ # # $Id: sccnt.cgi,v 1.6 2006/11/19 05:39:21 Hideki Kanayama Exp $ # Copyright(c) 2005-2006, Hideki Kanayama, All Rights Reserved. use strict; use CGI::Carp qw(fatalsToBrowser); # カウントログファイル my $countfile = "count.log"; # 時間設定 my $localtime_en = 0; my $offset = 9; # IPデータファイル my $ipdata = "ipdata.log"; # 同一IPをカウントしない時間(分) my $expire = 30; print "Content-Type: text/html\n\n"; my $today = time; my $yesterday = $today - 86400; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)= $localtime_en ? localtime($today) : gmtime($today + $offset * 3600); my ($ysec,$ymin,$yhour,$ymday,$ymon,$yyear,$ywday,$yyday,$yisdst)= $localtime_en ? localtime($yesterday) : gmtime($yesterday + $offset * 3600); my @iparray; if (open (IPDATA, "< $ipdata")){ flock IPDATA, 2; @iparray = ; close(IPDATA); } my $thisip2 = $ENV{'REMOTE_ADDR'}; my $thisip = $thisip2; $thisip2 =~ s/\./\\\./g; my %iphash = map {chomp;split /,/} @iparray; my $hit; if (grep /^$thisip2$/, keys %iphash){ exit if ($iphash{$thisip} + $expire * 60 > $today); $iphash{$thisip} = "$today"; $hit = 1; } my @newip; foreach (keys %iphash){ push @newip, "$_,$iphash{$_}\n" if ($iphash{$_} + $expire * 60 > $today); } open (IPDATA, "<+ $ipdata") or open(IPDATA, "> $ipdata") or print "$ipdataを作成できません。"; flock IPDATA, 2; truncate IPDATA, 0; seek IPDATA, 0, 0; print IPDATA @newip; print IPDATA "$thisip,$today\n" if ($hit == 0); close(IPDATA); my $a_count = 0; my $y_count = 0; my $t_count = 0; if (open(FILE,"< $countfile")){ flock FILE, 2; while (){ chomp; my ($cmon,$cday,$cyear,$count)=split(/,/); if ($cmon eq 'init'){ $a_count = $count; } else { $a_count += $count; } if ($cmon == $ymon && $cday == $ymday && $cyear == $yyear) { $y_count = $count; } elsif ($cmon == $mon && $cday == $mday && $cyear == $year) { $t_count = $count; } } close(FILE); } $t_count++; $a_count++; my $initval = $a_count-$t_count-$y_count; open (RDFILE,"+< $countfile") or open (RDFILE,"> $countfile") or print "$countfileを作成できません。"; flock RDFILE, 2; truncate RDFILE, 0; seek RDFILE, 0, 0; print RDFILE 'init,,,' . $initval . "\n"; print RDFILE "$ymon,$ymday,$yyear,$y_count\n"; print RDFILE "$mon,$mday,$year,$t_count\n"; close(RDFILE); chmod 0666, $countfile;