#!/usr/local/bin/perl #!/usr/bin/perl # Change this above path to suit your perl path some servers # setting is one of these two paths, use only one as first line # of your cgi script. ###################################################### ###################################################### # Text Counter Version 2.1 # # # # Writen by: Tariq Ismail Dalvi # # Email: tariq@dalvis.com # # Websites Owner : http://www.dalvis.com # # http://www.dalvis.net # # http://www.namesregistrars.com # # # # # # # # DALVI'S GROUP...India. # # # ###################################################### ###################################################### # Put this tage in any page where you want counter to display # to call this script is # This Script is without any warrant from dalvis.com or DALVI'S GROUP...India # But if you face any problem you can contact me at tariq@dalvis.com # write me the type of problem you are facing with this script may be i can help to fix it. # Path to your Log directory chmod 777 # this will be your log directory to store information on page hits $data_dir = "/home/public_html/logs"; # Path to your Log file in logs directory chmod 777 # this will be your log file $index = "/home/public_html/logs/log.txt"; # change the domain names to reflect yours @valid_uri = ('http://yoursite.com/','http://www.yoursite.com/'); $show_link = "http://www.yoursite.com/"; #$show_link = " "; # No need to change any thing below this line # If you want to play around feel free to do so ############################################ # auto_create should be 1 so whenever you add tage to any new page # it will create new counter file by defult $auto_create = "1"; # If you dont want to displat the date page is created show_date should be 0 $show_date = "1"; # This is maximum time limit for lock file in seconds $lock_sec = "3"; $pad_size = "1"; # Print Content Type Header For Browser print "Content-type: text/html\n\n"; # Get the page location from the DOCUMENT_URI environment variable. $count_page = "$ENV{'DOCUMENT_URI'}"; # Chop off any trailing /'s if ($count_page =~/\/$/) { chop($count_page); } $count_page =~ s/\//_/g; $lock_file = "$count_page\.lock"; # Check Valid-URI to make sure user can use this program. &check_uri; # Check to see if file is locked by program already in use. &check_lock($lock_sec); # If the file exists, get the date and count out of it. Otherwise, if # auto_create is allowed, create a new account. If neither of these are # true, return an error. if (-e "$data_dir $count_page") { open(COUNT,"$data_dir $count_page"); $line = ; chop($line) if $line =~ /\n$/; close(COUNT); ($date,$count) = split(/\|\|/,$line); } elsif ($auto_create = 1) { &create; } else { &error('page_not_found'); } # Increment Count. $count++; $print_count = $count; # Get Count Length for use in padding. $count_length = length($count); # Pad the number if it is smaller than $pad_size. for ($i = $pad_size;$i > $count_length;$i--) { $print_count = "0$print_count"; } # Print the Count, Link and Date if ($show_date == 1) { if ($show_link =~ /http:\/\//) { print "This Site have been visited $print_count times since $date"; } else { print "This Site have been visited $print_count times since $date"; } } else { if ($show_link =~ /http:\/\//) { print "This Site have been visited $print_count times"; } else { print "$print_count"; } } # Open the count file and write the new count that has been incremented. open(COUNT,">$data_dir/$count_page") || &error('could_not_increment'); print COUNT "$date\|\|$count"; close(COUNT); # Remove Lock File for next time script is run on that HTML page. &clean_up; sub check_uri { $uri_check = "1"; foreach $uri (@valid_uri) { if ($ENV{'DOCUMENT_URI'} =~ /$uri/) { $uri_check = "1"; last; } } foreach $uri (@invalid_uri) { if ($ENV{'DOCUMENT_URI'} =~ /$uri/) { $uri_check = "0"; last; } } if ($uri_check == 0) { &error('bad_uri'); } } sub create { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); @months = ("January","February","March","April","May","June","July", "August","September","October","November","December"); @wdays = ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); $year += 1900; $date = "@months[$mon] $mday, $year"; $count = "0"; open(COUNT,">$data_dir/$count_page") || &error('count_not_created'); print COUNT "$date\|\|$count"; close(COUNT); } sub error { $error = shift(@_); if ($error eq 'page_not_found') { print "[TextCounter Fatal Error: This Page Not Found\; Auto-Create Option Disabled]"; } elsif ($error eq 'bad_uri') { print "[TextCounter Fatal Error: This Page Not In Valid URI]"; } elsif ($error eq 'count_not_created') { print "[TextCounter Fatal Error: Could Not Write to File $data_dir/$count_page]"; } elsif ($error eq 'could_not_increment') { print "[TextCounter Fatal Error: Could Not Increment Counter]"; } exit; } sub check_lock { $time = $_[0]; for ($i = 1;$i <= $time; $i++) { if (-e "$data_dir/$lock_file") { sleep 1; } else { open(LOCK,">$data_dir/$lock_file"); print LOCK "0"; close(LOCK); last; } } } sub clean_up { unlink("$data_dir/$lock_file"); } ################################################## # Just a couple of configurations for you to set # ################################################## # Define the time zones verbiage you would like to display. my $daylight_zone = 'Pacific Daylight Time'; my $standard_zone = 'Pacific Standard Time'; # Define the server offset, if any. my $daylight_offset = -6; my $standard_offset = -6; ########## Nothing to change below ########## my @days = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday); my @months = qw(January February March April May June July August September October November December); my $dst = (localtime(time))[8]; my $tz_offset = $dst ? $daylight_offset : $standard_offset; my $zone = $dst ? $daylight_zone : $standard_zone; my ($sec, $min, $hour, $mday, $mon, $year, $wday) = (localtime(time + ($tz_offset * 3600)))[0..6]; my $ampm = ($hour < 12) ? 'AM' : 'PM'; $hour = 12 if $hour == 0; $hour -= 12 if $hour > 12; $year += 1900; $_ = sprintf("%02d", $_) foreach ($sec, $min, $hour, $mday); $date2 = "$days[$wday] $months[$mon] $mday, $year - $hour:$min:$sec $ampm,"; open(LOG,">>$index") || die "Can't Open User Error Log: $!\n"; print LOG "$date2 - $ENV{'REMOTE_ADDR'} - $ENV{'DOCUMENT_URI'} - "; print LOG "$ENV{'HTTP_REFERER'}\n$ENV{'QUERY_STRING'} - $ENV{'HTTP_USER_AGENT'}\n\n"; close(LOG); exit;