As we covered in our eXtended SSI
Tutorial: Part I, tailoring content to your reader's browser
is a very useful function.
In this tutorial, you will be introduced to several new and important
Before we get to work, of course I have a parlour trick for your
- The ELIF directive. For non-programmers, a funky concept,
but I know you will get the hang of it.
- The SET directive that allows you to place values into
variables that can be used later in your page.
- More complex logic, testing for more than one condition with
&& and ||. (AND and OR)
Your Customized Page Segment
Oh well, your browser did not match any of the tests that
my server side includes are checking for. I do hope that your
browsing experience is nonetheless pleasurable.
What Exactly is an ELIF?
Be careful reading the source code. The \/ make look
Why its the last thing a person says as an elephant sits on him!
It should only be that simple... The ELIF is used to nest
logic. It literally means ELSE IF. Thinking outloud:
This is easier than creating a seperate IF-ENDIF for every test. It
also allows us to have our handly ELSE catch-all at the end. In the
source code box that follows, you will see why this type of logic
If it is a dog
else if its a cat
else if its a bird
then don't say anything
Both Netscape and MSIE use the string Mozilla in their
HTTP_USER_AGENT. With WebTV, they use the string MSIE
in addition to WebTV. Getting some idea how much fun
this logic stuff can be? Here is how we attack the problem.
It is easiest to start working with the value that is most
unique. In this case it is WebTV. If we placed the WebTV
test below the MSIE tests, it could match on MSIE 3
and return the wrong result. So, if is not WebTV, we start
checking for MSIE.
Why MSIE first? As we just discovered, both MSIE and Netscape use
the word Mozilla in their HTTP_USER_AGENT. Therefore we
must again pick the most unique case first.
After we have exhausted our tests for MSIE, we then start checking
for Netscape by looking for Mozilla where we do not have
MSIE. Before we go further, please take a moment to examine
the source code below.
Note: The example below is general in nature. There are many
variations in browser names, and this example may not be specific
enough for your needs.
like a V, but it is really backslash-forwardslash.
I See A Lot Of Weird Stuff in there
Sorry about that. Sometimes the best way to eat is to start with
desert. (at least my kids think so) Here is what is happening:
For each possible browser that we are testing for, we use a SSI
SET directive to place a value in a variable, or temporary
holding bucket. In this example our bucket is named brtype.
Finally, we have some sort of value in our brtype bucket.
The final line then uses that value to create an INCLUDE
directive to merge in a conveniently named file.
Once we are done checking for MSIE, we start looking for Mozilla
where our brtype does not match with MSIE. These hopefully are our
Netscapes. The != means NOT MATCHING, and the && is
computerese for AND. We do not use it in this example but
you should be aware that || (two pipe characters) means OR.
AND means that both conditions must be TRUE, and OR
means that one or the other, or both must be TRUE for the IF
After we are done with our testing, we have an else to
handle anyother possible result such as Lynx and other less
often seen browsers and the ever growing family of spiders.
What Else Can I Do With This?
A great deal! You can use a logic structure such as this to set
values for everything from colors to text to whether or not to
Keep in mind that since the page is parsed and created before
being sent to the browser, that you can even tailor the <HEAD>
section of your page <BODY> tag. This is an easy way to
customize the major settings of a page for each browser.
If you were to place that source code at the very top of your file,
you could very easily do this:
What Happens If I Do Not Use the Slashes?
<!--#if expr="$brtype = /msie/" -->
<!--#elif expr="$brtype = /netscape/" -->
Using the /slashes/ means you want to match a pattern. If
you leave the slashes off, then it must be a perfect match
to be TRUE. In our example code, we have three possible
values for netscape: 1, 2, 3,& 4. Therefore if we do
not use the /slashes/ we must test for netscape4 or netscape2
Note: Browser versions change all the time, so even keeping
up with this can be a chore. This example does not take all browser
possibilites into account.