Including a page hit count in a web page

Note: we don't recommend including page reference counts. The counts are inevitably inaccurate, and generating them requires additional work for the web server, with very little benefit. However, if you insist ...

There are several ways to get a page count in your HTML page. In all cases, the basic idea is that an access to your web page causes a script to be run which updates the current count and returns the new value to be included in your page. The most common ways to do this are:

Cookbook #1 (server-side includes)

Scripts like these depend heavily on the details of the particular server configuration. The following easy (but not necessary good) technique is specific to darkwing.uoregon.edu or gladstone.uoregon.edu.

1. Create a subdirectory public_html/cgi-bin. In that directory, place the following simple program. Let's call it simplecount.cgi:

    #!/bin/sh
    FILE=`basename "$1"`.count
    echo "content-type: text/plain"
    echo ""
    read COUNT < $FILE || exit
    COUNT=`expr "$COUNT" + 1`
    echo "$COUNT" > $FILE
    echo "$COUNT"

Make sure that your program is executable, e.g. by giving the command:

    % chmod u+x simplecount.cgi

2. Let's call your page something.html or something.shtml. .shtml is easier, but doesn't work if you want a count in your index page, which has to be index.html. In your page, include some text such as:

    You are visitor number <b>
    <!--#include virtual="/~yourid/cgi-bin/simplecount.cgi?mycount" --> </b>.

Replace yourid with your darkwing/gladstone username, simplecount.cgi with the name you gave your program, and mycount with a name for this counter.

3. Create a count file corresponding to your counter to hold the current page count. If you are calling this counter "mycount," then in your cgi-bin directory:

    % echo 0 > mycount.count

Note that on darkwing/gladstone the script runs as you, and so has full access to all your files (a security problem if you have errors in your script). On other systems a similar script might run as an anonymous user, so you would need to create a world-writeable count file -- a security problem since then a malicious user could arbitrarily change the file.

4. Normally, only .shtml files get the special (and expensive) processing needed to recognize server-side includes such as the above. If your file is something.html, create a file name .htaccess (note that since the name starts with a period it will not normally be shown by an ls command) in the same directory as your page, and put in it the single line:

    AddType text/x-server-parsed-html .html

As an example, this page has been visited 11174 times as of Thursday, 24-Jul-2008 02:50:22 PDT.

Cookbook #2 (cgi scripts in an IMG tag)

Writing a program that you can use in an IMG tag, and that returns a graphical counter, is not as trivial as the above textual program. However, there are various programs available for you to copy. Even easier, there are service providers on the Internet who will maintain your counts for you (e.g., http://www.digits.com/web_counter/). After initializing the counter, you need only make sure that your web page has an IMG tag that refers to the appropriate counter service. In many cases they provide not only a count, but also additional information for the page owner. For example, the page owner might be able to query the distribution of hits by type of web browsers.

An example of a very simple counter service is one provided by JQ Johnson. It is available only for pages at UO. You can use it by placing the following 2 lines on your web page:

    <IMG ALT="many" width=56 height=16
     SRC="http://darkwing.uoregon.edu/~jqj/cgi-bin/counter.cgi?
http://darkwing.uoregon.edu/~jqj/publishing/pagecounts.shtml">

Replace http://darkwing.uoregon.edu/~jqj/publishing/pagecounts.shtml with the URL for your page.

For example, this counter shows that this page has been visited many times

Caveats (some problems with doing page counts)

Additional references


jqj@darkwing.uoregon.edu
http://www.uoregon.edu:/~jqj/publishing/pagecounts.shtml
last modified Wednesday, 16-May-2001 06:42:58 PDT