Dynadot

Tutorial: Creating a simple cgi hit counter for your site using Perl

Spaceship Spaceship
Watch
Impact
6
Code:
        //
       //  
      //Tutorial by deadserious - © [url]http://www.webdesigntalk.net[/url]
     //© 2003 [url]http://www.webdesigntalk.net[/url] 
    //REPUBLICATION OF THE TUTORIAL REQUIRES OUR PERMISSION.
   //[email protected] 
  //
 //
//
Okay here goes my attempt at showing you how to create a simple CGI text based hit counter for your site with Perl.

Requirements:
Perl and cgi access
Cgi script execution
Server Side Includes
Code:
#!/usr/bin/perl

      ##Code by deadserious - © 2003 [url]http://www.webdesigntalk.net[/url]
     ##THIS CODE IS FREEWARE AND CAN BE USED FOR BOTH 
    ##COMMERCIAL AND NON-COMMERCIAL USE. REPUBLICATION
   ##OF THE CODE REQUIRES OUR PERMISSION. 
  ##[email protected]
 ##
##

print "Content-type: text/htmlnn";
$this = "counter.txt";
open (THIS, "$this") || die("Could not open file!");
$that = <THIS>;
close (THIS);
open (THIS, ">$this") || die("Could not open file!");
$that++;
print THIS "$thatn";
close (THIS);
print "$thatn";
exit;
Now were going to explain it line by line:

Line 1: #!/usr/bin/perl
This is the first line in the script it tells it where to find the perl interpreter so that the script can run.

Line 2: #Script wriiten by deadserious - http://www.webdesigntalk.net
This is a comment. Anything from an unquoted pound sign to the end of a line is a comment and will just be ignored.

Line 3: print "Content-type: text/htmlnn";
This tells the browser the content will be text/html.

Line 4: $this = "counter.txt";
We define a variable and give it an as is string value by surrounding it with double quotes, counter.txt is the path to the file we'll be using to log the hits to the page.

Line 5: open (THIS, "$this");
This opens the file so we can read the contents of it.
open (THEFILEHANDLE, "THEFILE");
We are using "THIS" as the file handle, and we defined the variable $this with the value of counter.txt in the beginning of the script. You can name the file handle whatever you want.
Also notice the || die("Could not open file!") this is a die routine, and says to exit the CGI if the file cannot be open.
You should always do this or you may end up with a CGI running continuously even if the file isn't open, and you could end up losing data.

Line 6: $that = <THIS>;
We save the opened file in to the variable $that so we can make use of it's content later and close the file.

Line 7: close (THIS);
We close the file using the same FileHandle we opened it with.

Line 8: open (THIS, ">$this");
We reopen the file for overwriting it. Notice the little > before $this, this tells it to open the file for overwriting it.

Line: 9 $that++;
Increase the count.
So remember before we saved the first opened files content into the variable $that, so that we could use it at a later time, now we increment the value of $that by 1.

Line 10: print THIS "$thatn";
Prints the new value of $that to the file.
Remember we opened the file with the FileHandle "THIS" so we need to print with the same file handle.

Line 11: close (THIS);
Closes the file, don't forget to do that!

Line 12: print "$thatn";
Prints the output to the browser.

Installing the scirpt:

1. Copy and paste the code into you favirote text editor, notepad will work just fine. Make sure the first line has the correct path to the perl interpreter. Save it as thecount.cgi

2. Upload in ascii mode to your cgi-bin and chmod to 755

3. Create a blank text file and save it as counter.txt
Upload it to your cgi-bin in ascii mode and chmod to 666

4. Create a new text file and add the folowing code to it:
Code:
<!--#exec cgi="/cgi-bin/thecount.cgi" -->
Save it as whateveryouwant.shtml, and upload to your web accessible directory usually something like public_html

5. Browse to the .shtml file you created and let us know if it works :D

If you want to use this in your php pages you can add this code where you want it to show up:
PHP:
<?php virtual("/cgi-bin/thecount.cgi"); ?>

You can change the fontsize and colors by adding tags like this:
Code:
<font color="#0000ff"><!--#exec cgi="/cgi-bin/thecount.cgi" --></font>

Well there's many different ways to create a simple cgi hit counter with Perl, and this is about as simple as it gets, but it works. ;)

I hope that gives you a basic idea and some inspiration to learn some PERL.
Keep checking back for more tutorials.

And remember there's more than one way to do it. :)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Nice Tutorial. But I can never seem to get CGI to work for me on any Host :blush:
 
0
•••
you should just be able to run them from the cgi-bin however some hosts dont allow the .pl extension so try it with .cgi
 
0
•••
I always get the same problem, I'll try what you said :)
 
0
•••
Back