Server Side Includes

When you request a page from the Webserver it normally just sends out the file as it sits on the disk. With our Web server this is not true all the time. You can actually instruct it to examine the content of your Web page and execute some simple commands before the page is sent out. For this to work you have to let the Web server know that you want it to parse your document. There are two ways of doing this:

  1. Set the name of the Web page to end in .shtml

  2. Set the user and group execute bits of the file: chmod ug+x index.html

There is a slight difference between these two methods: If your filename ends in .shtml, the Web server does not send any information about the document's age (no Last-modified header) along with the document. With the second method, the server will set the Last-modified ` header to the file's last modified time. The point is that Web proxy caches decide upon the age of the page how long it should remain in the cache.

So much for the theory, now what does this buy you? You can now for example add lines like these:

<!--#include virtual="/~joe/myfooter.shtml" -->

This Includes the content of another file into the document, and the other file again is parsed for SSI lines. (Note the `.shtml extension of the included file)

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

This shows the date when the document was last modified.

<!--#exec cmd="myhello.pl" -->

This will run the myhello.pl program in the current directory. The program has to be in the same directory as the document or in a subdirectory of it. The program will be executed under your own UID and thus the same security considerations should be observed as with CGI programs.

There are more things you can do with SSIs - check the Apache documentation at http://httpd.apache.org/docs/2.0/howto/ssi.html. The environment variables available in SSIs are the same as those in CGI scripts.

To get you started, here are two SSI example programs:

An access counter:

#!/usr/bin/perl
#
# A Simple Counter
#
umask 022;
$ENV{'PATH'} = "/usr/bin";
if ($ENV{SCRIPT_FILENAME} =~ /^(\/[^\s]+)$/) {
  $file=$1;
} else {
  print "[counter.pl ERROR: Can't determine SCRIPT_FILENAME]";
  exit 0;
}
$rema=$ENV{'REMOTE_ADDR'};
if (open IN, "$file.count") {
  @state=<IN>; #$state[0] is local $state[1] is remote
  close IN;
}
chomp @state;
$state[0] += 0;$state[1] += 0;
if (($rema =~ /^129\.132\./) || ($rema =~ /^192\.33\.93\./)) {
  $state[0] += 1 } else {
    $state[1] += 1 };
print  "$state[0] / $state[1]";
if(open (OUT, ">$file.count")){
  print OUT "$state[0]\n";
  print OUT "$state[1]\n";
  close OUT;
} else {
  print "[counter.pl ERROR: Can't write counter $file.count]";
  exit 0;
}

An access logger:

#!/usr/bin/perl
#
# Creates an individual Logfile for the page it is called from ...
#
umask 022;
$ENV{'PATH'} = "/usr/bin";
if ($ENV{SCRIPT_FILENAME} =~ /^(\/[^\s]+)$/) {
  $file=$1;
} else {
  print "[logger.pl ERROR: Can't determine SCRIPT_FILENAME]";
  exit 0;
}
$now = time;
if (open (OUT, ">>$file.log")) {
  if (-z $file) {
    print OUT "# GMT Time | seconds since 1970 | ".
      "remote address | remote name | referer address  | ".
        "useragent | remote user\n";
  }
  $ip_number = pack ("C4", split(/\./, $ENV{'REMOTE_ADDR'}));
  ($name) = (gethostbyaddr($ip_number, 2))[0];
  print OUT gmtime($now)."|$now|$name|$ENV{'REMOTE_ADDR'}|".
    "$ENV{'HTTP_REFERER'}|$ENV{'HTTP_USER_AGENT'}|".
      "$ENV{'REMOTE_IDENT'}\n";
  close OUT;
} else {
  print "[logger.pl ERROR: Problem with permissions of $file!]"
}

Copy these scripts into the directory where your homepage sits and make them executable. Then add an SSI line like: <!--#exec cmd="script.pl" --> to the page.


CategoryWEBS

Web/Homepage/ServerSides (last edited 2013-05-30 09:21:11 by pmeier)