Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Shell_Script_CGI

A simple, yet flexible hit counter for web pages

AN EXAMPLE OF HOW TO USE A SHELL SCRIPT WITHIN A CGI ENVIRONMENT

Example 1

Example 2

Example 1

Web page counters can be popular, but tend to be tricky to set up, and in many cases cause problems if the counter program is located on a different site than the page being counted. Counters can also slow web file transfer if a file must be accessed remotely or an image created on-the-fly. They can also interfere with cacheing of pages slowing retrieval of the same information and leading to net congestion. As a general rule, the writer of a web document is more interested in the access count than the reader. This document describes a simple way to create a counter which overcomes many of these problems that you can incorporate into your web page.

    What it does

  • Tracks hits on one or more pages
  • Can be displayed or hidden
  • Can be adjusted or reset
  • Count of one page can be displayed on another
  • What it doesn't do

  • Ordinal numbers ('th)
  • Odometer wheels
  • Commas in numbers over 1000

 

Note!!

This counter script and examples are written for the directory structure on west.net. You may need to alter the directory and path information for use on another server. The script may not work at all if your server does not allow user-defined CGI scripts. If you are using an ISP other than west.net and the counter does not operate, ask the web administrators at your site for help.

 

The Script

This counter uses a shell script that must reside in your cgi-bin directory. The script creates a file the first time it is run, containing the number 1. Each subsequent time the script is run it looks for that file, gets the number, and increments it by one.

The script is called by your HTML code in the page being counted, and the output file is named there. Because you control the name of the file that is created with the count, you can use the same script to count multiple pages just by specifying a different output file. This also allows you to cheat . Here's the script, which can also be retrieved by FTP.

#!/bin/ksh

 

# This script creates a file showing the number

# of times it has been executed.  Useful for

# background counts of html accesses and the like.

# Written by Jay Hennigan (jay@west.net)

# Released to the public domain 1996.

 

echo

typeset -i counter=1

outfile=../public_html/$1

if test -s $outfile

then

  read counter < $outfile

  counter=$counter+1

fi

  echo $counter > $outfile

You will need to place the above code into a directory labeled

cgi-bin

immediately under your home directory. You can either

  • FTP the file to your home system and then FTP it back, creating the directory at the time if it doesn't exist
  • Copy the code and type it in manually with a unix editor
  • Log into your home directory and run the following commands:
  • mkdir cgi-bin

    chmod 711 cgi-bin

    cd cgi-bin

    cp /home1/ftp/pub/staff/jay/count count

    chmod 711 count

The count script file and the cgi-bin directory must both be executable in order to function.

 

Calling the script from the page to be counted

Once you've created the counter script, you now need to cause it to be run when a page to be counted is viewed. This is done by the following line inserted in the page:

<!--#include virtual="/cgi-bin/cgiwrap/username /count?filename"-->

Substituting your own username for username above, and also substituting a filename such as page.hits for filename above. If you're counting multiple pages, each one should have a unique filename.

For example, the code that counts this page is:

<!--#include virtual="/cgi-bin/cgiwrap/jay/count?countpage"-->

You also need to make the page to be counted executable. To do this, run the following unix commands, assuming that the page to be counted is page.html .

cd public_html

chmod 755 page.html

 

Displaying the count

EXAMPLES

It isn't necessary to display the count at all, bear in mind that such displays will slow down the access to your page, and may not be accurate if the page is cached. As a rule the count is more interesting to you as the author than to the reader. If you choose not to display the count, you can always go to your public_html directory and read the file page.hits .

To display the count on a web page, we can include the file in a line of text, like this:

The page page has been accessed <!--#include file="page.hits"--> times.

Example 2

Call of a CGI script file


<A HREF="http://www.mc.com/cgi-bin/dynamic_page">Dynamic page</A>

A simple CGI script on a unix based web server to return a list of the current users who are logged onto the web server is as follows: is:

#!/bin/sh

echo Content-type: text/html

echo echo echo "<HTML>"

echo "<HEAD>" echo "

</HEAD>" echo "<BODY>"

 echo "<H2>Users logged in are:</H2>"

 echo "<PRE>" who

echo "</PRE>"

echo "</BODY>"

 echo "</HTML>"

Remember:

  • The "'s around text with a < or > character.

On a Unix system:

ref http://snowwhite.it.brighton.ac.uk/~mas/mas/courses/html/html3.html#CGI-record

  • The first line is #!/bin/sh
  • The file h

    #!/bin/sh echo Content-type: text/html echo echo echo "<HTML>" echo "<HEAD>" echo "</HEAD>" echo "<BODY>" echo "<H2>Data recorded</H2> echo Use the back arrow on the browser echo to return to the original web page echo "</BODY>" echo "</HTML>"

    cat >> /home/snowwhite/staff/mas/log echo >> /home/snowwhite/staff/mas/log

     

    as executable permission set.

An example of its use is shown below:

<FORM METHOD="post" ACTION="http:/machine/cgi-bin/mas_cgi1"> <INPUT TYPE="text" NAME="name" SIZE=20 VALUE="fill in">