Server Side Includes: Conditional Operations
In this example we show how to use the Server Side Include (SSI) conditional operation and how it can be used in simulating a random image or include varying content. This operation also makes use of the Date (and Time) function of the local server with SSI. Apache refers to conditional operations as as flow control.
Conditional Example
The seconds of the current local time is: 37.
This time falls in the range of
30-39 seconds.
The following image is displayed as a result of this time range:

This example uses two conditional operations for illustrative purposes but the same result could be done in one conditional operation. However, you will notice from the examples how the amount of code can be reduced in each condition when it is written specifically for it's purpose.
Explanation of Use
| <!--#config timefmt='%S' --> |
You will notice that the SSI <!--# --> looks a lot like an HTML comment <!-- -->. The pound symbol is the key to how these are recognized.
| <!--#set seconds='$DATE_LOCAL' --> |
Note: The dollar sign is necessary to call the value of the variable, rather than assigning our new variable to be equivalent to the other variable.
Next we work with the conditional operation itself. The conditional operation supports the IF #if, ELSE IF #elif, and ELSE #else conditional cases and is concluded with an END IF #endif.
In each condition the expression must be assigned to the EXPR variable for the condition to be tested. If the condition compares to the content of another variable then that variable must be written in the form of: ${variable}, otherwise the character string itself will be tested.
Regular comparative operations can be used in the expression such as less than <, greater than or equal to >=, not equal to !=, and a number of other combinations. The comparisons can be mathematical or string literal.
String comparisons can be exact match:
(i.e. expr='${color} >= Red')
for an exact string match with Red; partial matches:
(i.e. expr='${color} >= /[Rr]ed/')
to match any occurrence within the string to Red or red;
or any combination of regular expression pattern matches.
Example Code
Finally this is the example SSI code used to display the time range:
|
<!--#config timefmt='%S' --> This time falls in the range of <!--#if expr='${DATE_LOCAL} >= 50' -->50-59<!--#elif expr='${DATE_LOCAL} >= 40' -->40-49<!--#elif expr='${DATE_LOCAL} >= 30' -->30-39<!--#elif expr='${DATE_LOCAL} >= 20' -->20-29<!--#elif expr='${DATE_LOCAL} >= 10' -->10-19<!--#else -->00-09<!--#endif --> seconds. |
|
<?php $sec = date('s') ?> This time falls in the range of <?= ($sec >= 50)? '50-59' : (($sec >= 40)? '40-49' : (($sec >= 30)? '30-39' : (($sec >= 20)? '20-29' : (($sec >= 10)? '10-19' : '00-09')))) ?> seconds. |
This is the code for the SSI pseudo random image used above:
|
<!--#config timefmt='%S' --> <img src="images/<!--#if expr='${DATE_LOCAL}>=50' -->cat<!--#elif expr='${DATE_LOCAL}>=40' -->cow<!--#elif expr='${DATE_LOCAL}>=30' -->dog<!--#elif expr='${DATE_LOCAL}>=20' -->finch<!--#elif expr='${DATE_LOCAL}>=10' -->starfish<!--#else -->swan<!--#endif -->.jpg" width="350" height="250" alt="The simple life of animals."> |
|
<?php $sec = date('s') ?> <img src="images/<?= ($sec >= 50)? 'cat' : (($sec >= 40)? 'cow' : (($sec >= 30)? 'dog' : (($sec >= 20)? 'finch' : (($sec >= 10)? 'starfish' : 'swan')))) ?>.jpg" width="350" height="250" alt="The simple life of animals." /> |
Please note: A space, or carriage return, after each SSI statement of #if, #elif, #else, and #endif MUST remain or the condition will fail. You also must not have a space between the <!-- and the # or the statement will not be interpreted as an SSI command. Most of the other spaces used in these examples are optional for readability. In the image example spaces between the conditions would result in the image name then containing a space and the image will not load.
