|Webmaster Tutorials Instructional webmaster-related how-to's and tutorials.|
|2 members in live chat ~|
| ||LinkBack||Thread Tools|
|02-07-2003, 10:46 PM||THREAD STARTER #1 (permalink)|
Join Date: Aug 2002
Tutorial: Creating a simple cgi hit counter for your site using Perl
// // //Tutorial by deadserious - © http://www.webdesigntalk.net //© 2003 http://www.webdesigntalk.net //REPUBLICATION OF THE TUTORIAL REQUIRES OUR PERMISSION. //email@example.com // // //
Perl and cgi access
Cgi script execution
Server Side Includes
#!/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. ##firstname.lastname@example.org ## ## 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;
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:
<!--#exec cgi="/cgi-bin/thecount.cgi" -->
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:
<font color="#0000ff"><!--#exec cgi="/cgi-bin/thecount.cgi" --></font>
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.
|04-18-2004, 01:19 PM||#2 (permalink)|
Nice Tutorial. But I can never seem to get CGI to work for me on any Host :blush:
|04-21-2004, 07:41 AM||#3 (permalink)|
Join Date: Apr 2004
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
|Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)|