NamePros.com  

Webmaster Tutorials Instructional webmaster-related how-to's and tutorials.


Views: 6081
Closed Thread
 
Thread Tools
Old 02-07-2003, 10:46 PM THREAD STARTER               #1 (permalink)
Senior Member
deadserious's Avatar
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 




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


Code:
        //
       //  
      //Tutorial by deadserious - © http://www.webdesigntalk.net
     //© 2003 http://www.webdesigntalk.net 
    //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 http://www.webdesigntalk.net
     ##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

If you want to use this in your php pages you can add this code where you want it to show up:
PHP Code:
<?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.
deadserious is offline  
Old 04-18-2004, 01:19 PM   #2 (permalink)
NP Maniacâ„¢
Phoenix's Avatar
Join Date: Apr 2004
Location: London, UK
Posts: 819
Phoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to behold
 
Send a message via MSN to Phoenix




Nice Tutorial. But I can never seem to get CGI to work for me on any Host :blush:


47ideas.com - my portfolio.
Phoenix is offline  
Old 04-21-2004, 07:41 AM   #3 (permalink)
New Member
kendo's Avatar
Join Date: Apr 2004
Posts: 7
kendo is an unknown quantity at this point
 




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
kendo is offline  
Old 05-29-2004, 01:31 AM   #4 (permalink)
NP Maniacâ„¢
Phoenix's Avatar
Join Date: Apr 2004
Location: London, UK
Posts: 819
Phoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to beholdPhoenix is a splendid one to behold
 
Send a message via MSN to Phoenix




I always get the same problem, I'll try what you said


47ideas.com - my portfolio.
Phoenix is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools




All times are GMT -7. The time now is 01:32 AM.



Domain name forum recommended by Domaining.com
Powered by: vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO
Don't use this form or this link. If you do, you'll automatically be blocked. nofollow