How to count XNet page accesses for the 20270th time since 1/1/99

My experience is primarily with NCSA, Netscape and Apache servers. Cern servers may require a CGI wrapper to parse pages for server side includes (SSI).

If you save this page for later local viewing with your browser, change the filename extension to .htm or .html

For more on scripts in general see my [Script FAQ]

If you have trouble using just a filename in the same directory for the server side include, see the [*Broken Counter] info below to find the real Web path.


Setting up the Counter

mv counter counter.cgi

#! /usr/bin/perl
#! /usr/local/bin/perl

NOTE: Do a 'type perl' or 'which perl' to find perl on a UNIX system.

chmod 755 counter.cgi

touch access_count
chmod 666 access_count

NOTE: On XNet it is no longer necessary to have world write permission for files written to since CGI scripts now run as the user.

ln counter.cgi counter-ord.cgi
or
ln -s counter.cgi counter-ord.cgi

NOTE: Symbolic links (-s) are still good if an updated file is uploaded. Hard links (without -s) remain good if one of the linked files is edited online, but if an updated file is uploaded to one of the links, the other links still point to the old file. Our webserver now requires hard links, so check link file sizes and dates after any modifications.


Testing the Counter

If you edit the script in DOS, see [note] first.

If you want to test a script from UNIX or get it to run by just typing the filename you may need to use a "./" prefix if the current directory (.) is not in your path (set in '.profile' or '.cshrc'). Make sure you back up any file before changing any dot files and if the path word wraps, suck it back into one line before saving.

You may have to do something with rehash or hash (see the 'man') if you write a new script and want to run it immediately (not needed if you ftp upload and then login to the shell). For more info see "Open Computing - UNIX Unbound" from Osborne/McGraw-Hill.


Using the counter on a page


Tell the server to parse the page for SSI

Note: You may not be able to use the counter on or rename your index.html home page. Our apache server is set to look for and parse an index.shtml if there is no index.html For some servers (NCSA and Apache, but not Netscape) it may be possible to set the server to parse *.html files by adding an '.htaccess' file.

Note that the contents of the .htaccess file will apply to that directory and any under it. The .htaccess method is not recommended for any files that the public can write to (like guestbooks).

Example of .htaccess entry to parse all html files:
AddType text/x-server-parsed-html .html

Apache can use that or:
AddHandler server-parsed .html

Example .htaccess to only parse html with execute bit set (755):
XBitHack on
(have not tested with Apache suexec)

print "Content-type: text/html\n\n";

Typical SSI tags (* see broken counter below)

Examples

<!--#exec cgi="./counter.cgi"--> for same dir as page
<!--#exec cgi="/~foo/counter.cgi"--> for foo/public_html
<!--#exec cmd="counter.cgi"--> same dir as page
<!--#exec cmd="/full_system_path_to/counter.cgi"-->
(last one does not work with Apache suexec)

If using an html editor you may need to use an html markup mode or load the file as plain text to add this.

When testing scripts or new pages, you may need to turn off and clear the page cache in your browser or you may be viewing an old cached version or cached error result. Although, hopefully the reload button works properly for most browsers now.


* Broken Counter

If you edit a script in DOS, then you will need to strip off the carriage returns before it will run in UNIX. The best way to make sure that lines terminate properly is to upload as ASCII. For a script to fix DOS files for Unix, download [dos2unix.pl]. You can shorten the name to "dos2unix", "chmod 755 dos2unix" and "./dos2unix counter.cgi".

Another quick way to fix a DOS file in the Unix shell is to load it into 'pico', [space], [backspace], Ctrl-X, Y, [enter]. In other words add a space, remove it, save the file and lines will be terminated properly.

NEW NOTE:

A recent server change may have broken server side includes on XNet. This is due to the 'suexec' wrapper that runs CGI scripts as the user and no longer allows a cmd= path to start with '/' or '..' so you can no longer specify a full path. Also make sure that scripts do not have write permission (no 777).

While you can still use 'cmd=' (with no path) for scripts in the same directory, to address scripts in a higher directory you need to use 'cgi=' with a web path. For example I can use my public_html/counter.cgi from a page in any directory by using the tag:

<!--#exec cgi="/~efflandt/counter.cgi"-->

For some systems your Web path may differ from your shell path. For a script to determine the actual path to your Web directory, create the following "testpath.cgi" script (chmod 755 testpath.cgi). Do it with a UNIX editor if possible. If you edit it in DOS see my [dos2unix.pl] perl script to strip off unwanted carriage returns.

Test it from UNIX (try './testpath.cgi' if you have not set the current dir in your path). Once it runs from UNIX you may get different results when run from the Web.

#!/bin/sh
echo Content-type: text/plain
echo
echo testpath.cgi shows path to this directory:
echo
echo PATH = $PATH
echo
echo current pwd path =
pwd
echo
echo Is perl in the path?
type perl
echo
echo Is perl in this path?  /usr/bin/perl
type /usr/bin/perl
echo
echo Is perl in this path?  /usr/local/bin/perl
type /usr/local/bin/perl
echo
echo Is perl in this path?  /usr/local/gnu/bin/perl
type /usr/local/gnu/bin/perl

XNet now has Perl5 available at any of these paths:
/usr/bin/perl (other systems may have perl4 here)
/usr/local/bin/perl
/usr/local/gnu/bin/perl
(I have not checked this)

You should now be on your way to counting accesses to your pages.


This page was last updated 1/1/99

If you have any questions you can contact me at:

efflandt@xnet.com

Home Page