1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Perl Hit Counter

Discussion in 'Perl' started by naveen, Jun 16, 2005.

  1. naveen

    naveen New Member

    Joined:
    Jun 2, 2005
    Messages:
    39
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Calcutta, India
    Home Page:
    Hi,
    Perl has been very famous for providing with very small but very useful programs, especially for the web. Hit Counters are one of such domains. Here is a small perl code to implement a basic hit counter in a web page

    Code:
    #!/usr/bin/perl -wT 
    
    print "Content-type:text/html\n\n";
    
    $log = "count.dat";
    open GETCOUNT, "<$log" or die("Error in $0: $log - $!n");
    $hits = <GETCOUNT>;
    close(GETCOUNT);
    
    $hits++;
    print "$hits people have viewed this website";
    
    open COUNT, ">$log" or dienice("Cant open file to WRITE");
    print COUNT $hits;
    close COUNT;
    Remember to create a file called count.dat with a default value of 0 in the same directory as the above program to store the number of hits. If you want to incorporate the program into another webpage, just copy it into that web page. Also remember to chmod the permission of count.dat to writable, else the program won't work.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83

Share This Page