Dismiss Notice
Welcome to Our Community
Wanting to join the rest of our members? Feel free to sign up today.

SSI Overview

Discussion in 'General Webmaster Helpdesk' started by melkior_inactive, Apr 20, 2007.

  1. In my htaccess files tutorial I mentioned SSI and how to enable it.
    I thought I could write a short article on SSI.

    SSI or Server Side Includes are a way of adding dynamic content to otherwise static HTML files.
    Again, we're talking about Apache servers here. Please note that.

    Now, you might have seen static pages that display the time or similair things. This can be done via JavaScript but it can also be done by SSI.
    I've told you how to enable SSI now let's look at the possibilities.

    SSI directives look like HTML comments so if you don't have SSI on your site they just won't be displayed. But if you do, apache will search through the file, parse them, run them and display the output/results.

    SSI always look like this:
    <!--#tag variable=value-->
    SSI directives are always opened with <!--# and they are always closed with -->

    If you want to output a value, you would use:
    <!--#echo var="Hello"-->
    which is actually useless since it's content is static.

    But let's say that you want to print out the date:
    <!--#echo var="DATE_LOCAL"-->
    If you want to output when you last updated the file without actually keeping the track you could write something like this:
    The file was last updated on: <!--#flastmod file="index.html"-->
    That would display the date of the last modification of the index.html file.

    If you want to use the same header on all your html files and you want to avoid using frames you could do this:
    <!--#include virtual="/header.html" -->
    You could do the same for the footer, or a menu.

    Another side to SSI is running commands on the server. This is also the dangerous part. It's a great opportunity for hackers to run commands on your server. So you'll want to be careful.

    There's also a difference if your apache server is running on Linux or on Windows since the commands aren't the same.
    Let's say you want to display the output of the DIR command (the directory listing)
    On Windows it would be:
    <!--#exec cmd="dir" -->
    But most servers are running Linux so that should be:
    <!--#exec cmd="ls" -->
    I'll give you some more examples tomorrow.
  2. temi

    temi Facilitator Webmaster

    Looking good Melky :)
  3. Let's say you want to display the IP adress of the user browsing your site.
    You can do that by writing this in your code:
    <!--#echo var="REMOTE_ADDR"-->
    You can also include the output of CGI scripts in your cgi-bin directory like this:
    <!--#exec cgi="/cgi-bin/script.cgi" -->
    That would execute the script called script.cgi located in your cgi-bin directory and it would print out the script's output in the page.
    Usually used for visit counters or click counters.

    If you want to link to the referrer page add this code:
    <a href="<!--#echo var="HTTP_REFERER"-->">Referrer page</a>
    And finally the conditionals or the IF statement.
    It is usually used to determine user's browser and serve different versions of the page customized for the browser.
    It looks like this:
    <!--#config timefmt="%A"-->
    <!--#if expr="$date_gmt = Friday" -->
    Friday today, tomorrow's weekend.
    <!--#elif expr="($date_gmt = Saturday) || ($date_gmt = Sunday)" -->
    Weekend finally!
    <!--#else -->
    Another work day...
    <!--#endif -->
    The first line of the code defines that the date should display the day of the week.
    The rest of the lines compare the date to the set parameters and display text accordingly.

    Well, that's about it. This was a short overview and a few examples included.
    Hope you liked it.

Featured Resources (View All)

Share This Page