perl Perl Script
Download Simple Perl Hit CounterDownload Simple Perl Hit Counter
Download Count: 131
Date Added: Sunday, 03-Jul-11 21:42:51 CDT
Tags: perl, cgi, hit counter

A simple perl hit counter and logger, no bells or whistles, just counts every page hit, and does some simple logging. Also adds the proper ending to the displayed count, i.e. 1st, 2nd, 11th, etc.

Make sure the file is executable (chmod 755 file_backup.sh) and include it in any web page with the following code snippet: <!--#exec cgi="http://path to file/log_counter.pl"-->.

Depending on your web server settings, you may need to name your web page files with the extension of .shtml instead of .html.

File Name: log_counter.pl - Code Type: Perl
#!c:\perl\bin\perl.exe 
 
### ERROR LOGGING ##
use CGI::Carp qw(fatalsToBrowser);
BEGIN
{
use CGI::Carp qw(carpout);
my $error_log =
"./counter_error_log.txt";
open(LOG, ">$error_log") or die("Unable to open $error_log:
$!\n");
print LOG "Errors:\n";
carpout(\*LOG);
}
### END ERROR LOGGING ##
 
$counter_file = "counter.txt";
$log_file = "log.txt";
 
$gmtPlusMinus = 0; #real local time i.e.  gmt time zone
local(($sec,$min,$hour,$mday,$mon,$year)) = (gmtime);
$mon = $mon+1; 
$year = $year+1900; 
$today = sprintf("%04d%02d%02d%02d%02d%02d",$year,$mon,$mday,$hour,$min,$sec);
 
 
    open (COUNT, "$counter_file");
    $counter = <COUNT>;
    close (COUNT);
    open (COUNT, "> $counter_file");
    $counter++;
    print COUNT "$counter";
    close (COUNT);
 
    open (LOG, ">>$log_file");
    print "Content-type: text/plain\n\n ";
    print LOG "$today|$ENV{'HTTP_USER_AGENT'}|$ENV{'HTTP_REFERER'}";
    print LOG "|$ENV{'REMOTE_HOST'}|$ENV{'REMOTE_ADDR'}\n";
    close (LOG);  
    print "$counter "; 
    &ending;
        exit;
 
sub ending {
@digit = split(//, $counter);
 
if ( $digit[-1] == 0 ){ print " th"; }
if ( $digit[-1] == 1 && $digit[-2] == 1){ print " th"; }
elsif ( $digit[-1] == 1 ) { print " st"; }
if ( $digit[-1] == 2 && $digit[-2] == 1){ print " th"; }
elsif ( $digit[-1] == 2 ) { print " nd"; }
if ( $digit[-1] == 3 && $digit[-2] == 1){ print " th"; }
elsif ( $digit[-1] == 3 ) { print " rd"; }
if ( $digit[-1] == 4 ){ print " th"; }
if ( $digit[-1] == 5 ){ print " th"; }
if ( $digit[-1] == 6 ){ print " th"; }
if ( $digit[-1] == 7 ){ print " th"; }
if ( $digit[-1] == 8 ){ print " th"; }
if ( $digit[-1] == 9 ){ print " th"; }
}
 
Parsed in 0.041 seconds - Rate: 40.24 KB/s - GeSHi version: 1.0.8.10

Hope you find this helpful! If you have any questions, please leave a comment.


  • PHP
  • Scripts
  • MS
  • Office
  • Templates
  • and
  • Files
  • Assorted
  • Linux
  • Bash
  • Scripts
  • Perl
  • Scripts
  • PDF
  • FIles
  • MS
  • Excel
  • 2003
  • Template
  • employment
  • tools
  • job
  • search
  • tools
  • spreadsheet
  • Job
  • Search
  • Spreadsheet
  • Template
  • bash
  • shell
  • script
  • file
  • backup
  • linux
  • gzip
  • tar
  • File
  • Backup
  • Script
  • Transmission
  • shell
  • script
  • public
  • torrent
  • private
  • torrent
  • Bit
  • Torrent
  • Transmission
  • BitTorrent
  • Completion
  • Script
  • Transmission
  • shell
  • script
  • torrent
  • information
  • Bit
  • Torrent
  • Transmission
  • Bit
  • Torrent
  • Get
  • Torrent
  • Info
  • bash
  • shell
  • script
  • add
  • user
  • samba
  • samba
  • user
  • add
  • samba
  • user
  • linux
  • command
  • line
  • Add
  • System
  • and
  • Samba
  • User
  • geshi
  • pyrocms
  • php
  • codeigniter
  • PyroCMS/CodeIgniter
  • Geshi
  • Helper
  • File
  • geshi
  • pyrocms
  • php
  • codeigniter
  • GeSHi
  • Code
  • Highlighter
  • plugin
  • for
  • PyroCMS
  • perl
  • cgi
  • hit
  • counter
  • Simple
  • Perl
  • Hit
  • Counter
  • pyrocms
  • codeigniter
  • php
  • ip
  • address
  • session
  • userdata
  • PyroCMS
  • IP
  • Address
  • Plugin
  • Soyo
  • SY5SSM
  • Socket
  • 7
  • Motherboard
  • Quick
  • Start
  • Guide
  • Soyo
  • SY5SSM
  • Socket
  • 7
  • Motherboard
  • Quick
  • Start
  • Guide
  • Home
  • web
  • designit
  • supportlinuxphpbash
  • PRIVACY
  • Statement
  • LEGAL
  • Disclaimer
  • Ebay
  • Listings
  • Soyo
  • Motherboard
  • PDF
  • Why
  • I
  • Like
  • the
  • HoneyPi
  • Brand
  • IR
  • Receiver
  • Module
  • for
  • the
  • Raspberry
  • Pi
  • Using
  • GeSHi
  • Highlighter
  • with
  • PyroCMS/CodeIgniter
  • Easy
  • as
  • 1
  • 2
  • 3
  • PHP
  • Stuff
  • Raspberry
  • PI