WN home page

Version 2.2.4
[Up] [Top]

WN Server Side Includes that Execute a Program


Is it possible to do a server-side include such as the following:

<!-- #echo var="LAST_MODIFIED" -->

I've tried it numerous ways and failed every time -- so I figured it was time for a definitive answer. I can certainly live without them but they would be nice to have.

1 How it Works

This is done with a server side include that executes a program. To see this example in action try index.html.

Here is an example of how to use scripts as server side includes. This is not a CGI/1.1 program, but something similar could be done with CGI/1.1. Assuming that the program "counter.cgi" is in the same directory as "foo.html" put something like:

File=foo.html
Includes=!counter.cgi

in your index.wn file. Then put:

<!-- #include -->

on a line with no leading whitespace in the file foo.html. The program counter.cgi gives the current count of accesses to this page and also prints the last-modified date. It should be easy to modify for your needs.

There are a couple of permissions issues. The user id under which the server runs (e.g. "nobody") must have permission to execute counter.cgi and to read and write the file /tmp/wncount.

2 The Program counter.cgi

Here is the perl program:

#!/usr/bin/perl

require "stat.pl";
require "ctime.pl";

# This perl program counts accesses to a file foo.html and prints
# the last modified date for that file.  Set the variable $file
# to the complete path of the file whose accesses you want to count.
# $countfile is a file which will contain the current count.  The complete 
# filename must be given for it too.  A careful version of this program
# would do file locking since multiple processes might be trying to
# update $countfile simultaneously.  Note that the WN user id (usually
# "nobody") must have write permission for this file.

$countfile = "/tmp/wncount";
$file = "$ENV{WN_DIR_PATH}/index.html";

&Stat( $file);

if (! -e $countfile)
{
  open(COUNT, ">$countfile") || die "Cannot open file: $! for writing";
  print COUNT "0";
  close(COUNT);
}

open( COUNT, "<$countfile" ) || die "Can't open file: $! for reading";
$count = ;
close( COUNT);
$count++;

print "

\n"; print " You are viewer ", $count, " to see this page.\n"; print "

\n"; print "

\n"; print " It was last modified ", &ctime($st_mtime), "\n"; print "

\n"; open( COUNT, ">$countfile" ) || die "Can't open file: $! for writing"; print COUNT $count, "\n"; close (COUNT);

WN version 2.2.4
Copyright © 1998 John Franks <john@math.nwu.edu>
licensed under the GNU General Public License
Last modified: Sat, 11 Sep 1999 13:09:13 GMT
[Up] [Top]