Using a Server-Side Include counter on your web page

This code has been compiled and used on HPUX here at Chico State. If you're elsewhere, your mileage may vary. Here are the steps to set this up:

  1. Make sure server-side includes are running on your server
  2. Get the counter.c program
  3. Modify and compile it
  4. Touch the counter file
  5. Add the server-side include directive to your web page
  6. Rename your web page, if necessary
  7. Set permissions

Make sure server-side includes are running on your server

If they aren't, this information is useless. If you don't know for sure, you can try digging through your http server's config files to see for yourself. Or you could contact your webmaster.

Get the counter.c program

This is easy. Just click on the above link and get the program. Stick it in the directory you want to have the counter in.

Modify and compile it

This might be easy, depending on which flavor of Unix you're running. If it barfs on the lockf(), you might have to use flock() instead. (Look it up in the man page.) If neither work, you can lock the counter file with fcntl(). If that doesn't work, well, just chop the locking code out of there--you probably don't even need it.

Oh! You for sure need to edit the line at the top of counter.c that defines where your counter file is (".counter",the one that holds the count of the visitors.) Then you can compile it.

On HPUX, it compiles with:

    cc -Ae -o counter counter.c
But most compilers can leave off the "-Ae" nonsense.

Touch the counter file

You need to create the counter file before your counter will work. Take the exact same file name and path you specified in counter.c on that #define line and touch it:
    touch /home/foo/public_html/.counter

Add the server-side include directive to your web page

You need to add a specialized comment tag to your web page right where you want the count to show up. This comment will run the counter program, and put everything that the counter program prints right there in the web document. The counter program is going to print a number, which is the count of the visitors so far.

Here is the comment to add:

    <!--#exec cmd="/user/foo/public_html/counter" -->
Whereever you put that in your web page, the count will appear instead. Mind you, that path points to the compiled counter.c file, not the .counter file you touched earlier.

If you're using Netscape's server, you might want to add this comment before the counter comment:

    <!--#config errmsg="" -->
That prevents some crazy error output that Netscape is fond of. We don't use Netscape's server here at Chico, so we don't need this comment.

Rename your web page, if necessary

Some servers, like ours at Chico, only process server-side includes if the web page has a specific extension. This is to save the http server from crunching through every single web page, most of which will not have any server-side includes. Again, check out the http server config files or talk to your webmaster if you're unsure about this naming convention.

You must use the extension ".shtml" on your web pages instead of ".html" or the server-side include will not inject anything into your page. For instance, my home page is "index.shtml" so the counter works. If I rename it to "index.html", the counter will cease to function.

Set permissions

Since the http server is going to run your counter program, it's going to run as httpd (or whatever user your server runs as) and not as you. This presents a problem, since no one else can update the .counter file without permissions.

There are two solutions, but you should only implement one of them:

  1. Make your counter program SUID
  2. Set the permissions on .counter to world-writable

You can make your counter program SUID by doing this:

    chmod 4755 counter
or you can make your .counter file world-writable by doing this:
    chmod 666 .counter
That's it!
Copyright © 1996 Brian "Beej" Hall

<- Back to the Chico Hackers Guild Web info page