|
||||
|
||||
|
Server Side IncludesMany people have asked about Server Side Includes (SSI). Typical questions are what are they, what do they do, and most how to make use of them. We'll go into some ways to use SSI in a way that you might be able to use to enhance your existing websites and/or to save time doing repetitive tasks. This is generally done better using PHP in my opinion, but for minor tasks SSI can be very handy. So what is this SSI stuff?Server Side Includes are relatively somple serverside commands that can be inserted into SHTML (or HTML) pages, to execute CGI programs, insert or include files, generate a date and time stamp, among other things. They are not usually available if you are using free hosting, because you have to be able to run CGI scripts locally; that's where the SSIs generate from. Also, some hosts disable some or all SSI commands, even though they allow CGI, for secutiry reasons. So first and foremost, find out if you have SSI enabled. What does "serverside" mean?Very simply put, when you hit a website with your browser, your browser and the server that the website lives on do a "handshake"; the browser asks for a certain url (page) and the server digs it up and serves it to the browser, which renders it (translates the code) to your screen to view. If all you requested was a simple html page, the operation is now complete. However, if the page you requested is encoded entirely or in part using a serverside language like SSI, ASP or PHP, your browser isn't smart enough to understand it... so the server has to "translate" the SSI, ASP or PHP code into html, and after doing so, it then sends it to your browser. This "translation" takes less time than you may think, unless the code is poorly written. SSI is probably the slowest of the serverside types, but the scripts that most webmasters use are short one-liners, and it's seldom that more than 2 or 3 are used. As an aside, PHP code runs much faster than SSI, and even faster than ASP (Active Server Pages, by Microsoft). SSI is not even a programming language, just a set of commands. ASP is built on very old COM based architecture (like Windows), which is an overhead for the server, whereas PHP code runs in its own memory space. PHP programs can run on a great many platforms like Linux, Unix, Windows and Solaris, whereas ASP is mainly associated with Windows platforms. The exception is that ASP can run on a Linux platform with Linux help, using ASP-Apache on the server, though most hosts find thes system problematical because of the secrecy of MS and its code being closed. So, PHP, being based on the extremely fast and powerful C++ language is pretty much top dog now. The syntax used in PHP is quite similar to C/C++, and C/C++ is still considered the best programming language by many programmers. People who love this language would surely feel more comfortable with the syntax of PHP. ASP has a more Visual Basic kind of syntax that again is closely related to only Microsoft products, and is a slower interpreted language. Enough about my preferences for serverside stuff, though-- you came here to read about SSI, so we'll get on with it. Most hosts will have the server set up so that your .HTML (or .HTM) pages must be changed to .SHTML, if the pages are going to include SSI tags. On the Webmaster side, SSI tags are mostly straight-forward and simple things. Just remember that you will probably have to save your HTML pages which have SSI tags in them as SHTML. What can SSI do for me?Well, let's get to our first example and show you. SSI commands have this format when inserted into SHTML web pages:
Pretty simple, huh? The SSI command exec is used to execute scripts (usually Perl) that are already installed in the cgi. Essentially it would do the same thing as calling the script from the browser by typing http://www.domain-name.com/cgi-bin/scriptname.cgi. Common uses for SSI exec calls are counter scripts, to count your visitors, include files or executing a script to print the current date and time. The SSI tags themselves do not show up in the source view of the HTML when you do a right mouse click to see it. What you see instead, is the result of the executed SSI tag. Remember, SSI are not the scripts themselves, but a command that merely calls for the execution of a CGI script or to run a system command. Here are five other available SSI commands and their actions:
echo - inserts the variable values of an SSI into your web page flastmod - inserts a date/time stamp of when a file was last updated fsize - will insert the size of a file into your web page include - insert the content of an HTML file into your web page. Before answering that, be sure that youremember this: in the Linux-UNIX world, everything is cASe sEnsItIvE. You must keep all SSI commands in lower case as shown above. Also, the command syntax structure is mandatory. If you insert a space, or if you omit one, it simply will not work. Having made that important point, you can try out SSI by inserting the following tag into your SHTML code where you want the date and time to be inserted, using the flastmod SSI command. <!--#flastmod file="filename.shtml" -->This will serve your browser the server's current time and date in the default date format... again, the SSI will print the local time zone of the server you are using, not your local timezone.
Don't worry though, ou can change it. By using the SSI config command with the argument timefmt. An argument, by the way, is a programming term, meaning an item that the command must work on or modify. In the example below, we will configure the time format, timefmt, to only show the date, not the time... be sure that you change "filename.shtml" to the name of your own page... if you don't you'll only generate an error.
This, at the time I write this, resulted in:
So where did I get the %x thing from? Well, it's just one of some common formats that can be used with SSI. Below is a short list of some others. You can substitute and mix to come up with your own preferred format. %a -- abbreviated weekday name %A -- full weekday name %b -- abbreviated month name %B -- full month name %c -- locale's appropriate date and time %C -- default date and time format %d -- day of month - 01 to 31 %D -- date as %m/%d/%y %e -- day of month - 1 to 31 %H -- hour - 00 to 23 (military time) %I -- hour - 01 to 12 %j -- day of year - 001 to 366 %m -- month of year - 01 to 12 %M -- minute - 00 to 59 %n -- insert a newline character %p -- string containing AM or PM %r -- time as %I:%M:%S %p %R -- time as %H:%M %S -- second - 00 to 59 %t -- insert a tab character %T -- time as %H:%M:%S %U -- week number of year (Sunday is the first day of the week) - 00 to 53 %w -- day of week - Sunday=0 %W -- week number of year (Monday is the first day of the week) - 00 to 53 %x -- Country-specific date format %X -- Country-specific time format %y -- year within century - 00 to 99 %Y -- year as CCYY (4 digits) %Z -- timezone name. (PST, EST etc.) Be sure to note the use of the lowercase is different than the UPPERCASE. Try mixing the format around using the date above. For instance, to print the month and year, use the following SSI command: <!--#config timefmt="%B %Y" --> <!--#flastmod file="filename.shtml" -->This will show up like this:
You can also disguise an error message like "an error has occured while processing this directive". Just use the config errmsg argument. This will let only yourself know, and not your viewers, that there is a problem with your SSI tag, when you see the mesage you made up in place of the error notification.
Catching on? Good. Here are a few more:
<!--#echo var="DOCUMENT_NAME" --> <!--#echo var="LAST_MODIFIED" --> - Outputs the date and time the file was last modified. Exec: <!--#exec cgi="/cgi-bin/hits.pl"--> - Runs the server side script.hits.pl. <!--#exec cmd="ls -al" --> - Performs a directory listing. Fsize: <!--#fsize file="main.shtml"-> - Displays the size of the file "main.html". Flastmod: <!--#flastmod file="main.shtml"--> - Displays the date the file was last modified. Flastmod may also be used as follows: Last modified on <!-- #flastmod filer="main.shtml" -->. Include<!--#include file="more.html"--> - Includes the contents of the more.html file in the current file Well, that should get you started. Have fun! |