| |
Server Side Includes
![[ line ]](img/line.gif)
- Some servers provide you with the ability to embed server commands into your HTML documents in the form of special HTML tags. When the server processes your document, it will replace these server commands with text appropriate to the function of the tag. This allows you
to insert server environment variables such as the server hostname, client address, or hit count.
Server Side Includes (SSI) also allows you to insert the contents of an entire separate file, or even the output from a CGI application or script.
- The exec command is the most powerful of the Your Server SSI commands. It allows you to run a CGI application or plug-in and insert its results into the current document. You could also run multiple CGI applications or plug-ins from a single URL. There is no limit to the number of exec calls that you can make in a single document. The syntax is: <!--#exec virtual="/cgi-bin/mycgi.cgi"-->
- <!--#exec cgi="counter.cgi"-->
- <!--#exec cmd="counter.cgi "-->
- The include command allows you to include the contents of a file into the current document. This is particularly useful for including common parts of documents that are identical across multiple documents. For example, say all of your site's pages have a common footer that contains a button bar with links to other documents on your site. If the URL of one of these linked documents changed, you would have to update every document on your site to point to the new URL. An easier way is to use the include command.
- The include command takes one argument, which can be either a "virtual" or "file" argument. If the include command is unable to find the file to be included, it will strip the command from the output text, but will not replace it with anything.
- <!--#include virtual=" "-->
- <!--#include file=" "-->
- The echo command allows you to insert text into your HTML document. When Your Server encounters the echo command, it replaces the command with the specified value.
- <!--#echo var="(different variables for this one)"--> - See Environment Settings below:
- Making a Real Back Button
- Here is a cute server side include trick, a back button that actually takes you back to where you came from.
<A HREF="<!--#echo var="HTTP_REFERER"-->">BACK BUTTON</A>
If you came to this page directly, such as from a link in your e-mail or you hand-typed it in, then (none) would be the value for HTTP_REFERER.
- Displaying current Date and Time within cgi scripts
- Shows how to create a file and subroutine which will allow you to include SSI within your scripts. Specifically using DATE_LOCAL to display current date and time.
This subroutine can be easily modified to include files within scripts.
- CGI Environment Settings
These are variables that are set every time a CGI script is run. They are defined by both the server and client. Depending on the server and the context, they may or may not all be set.
| ACCEPT_LANGUAGE | The human languages that are acceptable to the remote client. |
| AUTH_TYPE | The authentication method used to validate the remote client. |
| CONTENT_LENGTH | The length of the attached information in the case of a POST or PUT. |
| CONTENT_TYPE | The content type of the attached information in the case of a POST or PUT. |
| DATE_GMT | The current Greenwich Mean Time, with format specified by the config command. |
| DATE_LOCAL | The current date, with format specified by the config command. |
| DOCUMENT_NAME | The name of the document requested |
| DOCUMENT_URI | The URL of the document requested |
| FORWARDED | The name of the proxy server through which this document is being processed. |
| GATEWAY_INTERFACE | The name/version of the Common Gateway Interface served on this HTTP server. |
| HTTP_ACCEPT | A comma separated list of mime types that are accepted by the remote browser. |
| HTTP_COOKIE | The cookie sent by the remote client. |
| HTTP_REFERRER | The URL of the page that the client followed to get to the current document. |
| HTTP_USER_AGENT | The browser the client is using to send the request. |
| LAST_MODIFIED | The date and time of the current document's last modification, with format specified by the config command. i.e. <!--#flastmod file="ssi.shtml" --> |
| PAGE_COUNT | Number of accesses to current document since server was brought on line. |
| QUERY_STRING | The raw query string sent from the remote browser. |
| QUERY_STRING_UNESCAPED | The unescaped query string sent by the client browser, all shell-special characters escaped with \. |
| REMOTE_ADDR | The IP address of the client |
| REMOTE_HOST | The domain name of the client, if the Use DNS option is set. |
| REMOTE_IDENT | The remote user name if supporting RFC931 identification. |
| REQUEST_METHOD | The method by which the current document was requested. |
| REMOTE_USER | The user name used to validate authentication from the remote client. Great for use in password protected sites. |
| SCRIPT_NAME | The virtual path of the script being executed. |
| SERVER_NAME | The hostname of your server, such as "www.yoursite.com". |
| SERVER_PORT | The IP port the HTTP server is answering on. |
| SERVER_PROTOCOL | The protocol implemented by the server, "HTTP/1.0" |
| SERVER_SOFTWARE | The name of the server application, "Your Server" |
| TOTAL_HITS | Total pages served by server since brought on line. |
- How to access them: this differs between languages. In Perl, they are stored in the %ENV hash, with the name of the environment setting being the key value in the hash. For example, to print the remote host of the visitor, do this:
print "Remote host: $ENV{'REMOTE_HOST'}.\n";
This Perl / CGI script will print out the all the keynames and their values that are set:
#!/usr/bin/perl
print "Content-Type: text/html\n\n";
print "<table>\n";
foreach $key (sort keys %ENV)
{
print qq|
<tr><th align="right">$key:</th><td>$ENV{$key}</td></tr>\n
|;
}
print "</table>\n";
Remember to use these settings with respect for others and their privacy :)
![[ line ]](img/line.gif)
[ Back to Top | Home ]
Copy & Copyright (c) 1998 - 2006 Creative Computing
Please Click To Visit Our Friends At:
|