• home
  • forum
  • my
  • kt
  • download
  • Perl/CGI: Hit Counter

    Author: 2008-08-15 06:53:31 From:

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

    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

    discuss this topic to forum

    relation tutorial

    No relevant information

    New

    Hot