| | |||||
| ||||||||
| CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| Senior Member Join Date: Aug 2002
Posts: 1,255
![]() ![]() | A simple cgi hit counter for your site using PerlCode: //
//
//Tutorial by deadserious - © http://www.webdesigntalk.net
//© 2003 http://www.webdesigntalk.net
//REPUBLICATION OF THE TUTORIAL REQUIRES OUR PERMISSION.
//webmaster@webdesigntalk.net
//
//
// ????: NamePros.com http://www.namepros.com/code/14530-simple-cgi-hit-counter-your-site.html 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.
##webmaster@webdesigntalk.net
##
##
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"; ????: NamePros.com http://www.namepros.com/showthread.php?t=14530 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" --> 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: Code: <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. |
| |
| | #2 (permalink) |
| New Member Join Date: Nov 2003
Posts: 2
![]() | I got it to work but... I got it to work but only as a single SHTML page. If install and display it as WHATEVER.SHTML, it looks great and works great. If I use frames and WHATEVER.SHTML is one of those pages in the frames, it will not display the counter data. Any ideas? Thanks in advance, JB |
| |
| | #3 (permalink) |
| NamePros Member Join Date: Jul 2003
Posts: 118
![]() | yea, dont use FRAMES!!!!!!!
__________________ Paulicon Web design - Your one stop shop |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |