AZForums: Perl/CGI: Hit Counter - AZForums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Perl/CGI: Hit Counter

#1 Guest_GreyFox_*

Group:
Guests

Posted 05 June 2006 - 04:49 AM

First, here is what we will make, or at least the source code

#!/usr/bin/perl -w
#Simple hit counter

#To include in a webpage (HTML), put <!--#exec cmd="PATHTOCGISCRIPT.cgi"--> 
#in the document, replacing PATHTOCGISCRIPT with the url to the CGI script 
#(this script).  For PHP, within the <?php and ?> tags, include
#"include("PATHTOCGISCRIPT.cgi");"  without the quotes.  Also, you need 
#to create a file named hits.txt for the script to access.  It can
#be blank

#Script copyright 2006 GreyFox of hacktek.net 

#Error 1 = Could not read file
#Error 2 = Could not save file

#VARIABLES

$file = "hits.txt";

#READING

open (COUNT,"<$file") or die "Counter error 1";
$hits = <COUNT>;
close COUNT;

#INCREMENTING

$hits++;

#CONTENT
print "Content-type: text/html\n\n";
print "$hits";

#SAVING

open (COUNT,">$file") or die "Counter error 2";
print COUNT $hits;
close COUNT;


Ok, so you want to make a simple hit counter that will be used as a Server Side Include?
Well it is not that hard. First, you need to open Notepad or some other text editor. I
prefer Notepad2.

OK, now copy the above source code and paste it into your text editor. Now save it as
something.cgi (replacing something with whatever name). Now upload the script through
FTP to your own webserver to the cgi-bin directory.

Now, when you want to use it,
To include in a webpage (HTML), put <!--#exec cmd="http://yoursite.com/cgi-bin/CGISCRIPT.cgi"-->
in the document, replacing PATHTOCGISCRIPT with the url to the CGI script (this script).
For PHP, within the <?php and ?> tags, include "virtual("http://yoursite.com/cgi-bin/CGISCRIPT.cgi");"
without the quotes.

Also, you need to create a file named hits.txt within the cgi-bin folder for the script to access. It can be blank

This post has been edited by GreyFox: 23 June 2006 - 04:46 PM


#2 User is online   Kobius Icon

  • «• 千と千尋の神隠し •»
  • PipPipPipPipPipPipPipPipPipPipPipPip
  • View blog
Group:
Administrator
Posts:
15,526
Joined:
25-August 05
Location:
1999

Posted 05 June 2006 - 07:57 AM

It's nice to see a perl tutorial. Good Job GreyFox.

Moved to the Web Tutorials forum.

«AZHome»
Make sure you check out AZHome - the flagship of our community
---
•» Follow me on twitter «•
---
Targets: [ 10,000 members || 600,000 posts || 1,000 images ]
0

#3 User is offline   Doomed Icon

  • Behind the Curtain
  • PipPipPipPipPipPipPipPipPip
Group:
Members
Posts:
3,031
Joined:
07-November 05

Posted 05 June 2006 - 04:25 PM

Ah some sever side scripting. Very nice.
Dark Voltage already came.
Thanks to W.B. Yates for the sig.
Posted Image
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users