Tech stuff - Mixing PHP and SSI

We urrently use this technique with PHP5 and Apache 2.x (2.4 currently). We historically used PHP for all our web work. We have decided to migrate to ruby for all our new web development but we still have lots of PHP stuff hanging around that we upgrade and modify as necessary.

Background

We regularly mix PHP and SSIs for the following reasons:

Laziness - we have a lot of historic SSI stuff lying around and do not want to change it. We prefer evolution to revolution.

Appropriateness. Not all systems are good at everything. We find that conditionally selecting 'lumps' of code to deliver browser specific pages (see server side browser sniffing) is a lot cleaner and easier with SSI.

Nesting PHP and SSI

The rules go like this:

You can invoke SSI files from within PHP but must use the PHP virtual() function not include(). Variables set within PHP are NOT available to SSI so our favorite 'wheeze' of supplying last modified dates to a standard footer do not work.

You can include SSI files using the include virtual SSI directive but the SSI filename must have a .shtml extension even if the XBitHack is being used.

Note: We would guess that the Apache environment for each type of file (.php and .shtml) is initialised to the same state as when the page is first called, whereas a nested .php files uses the same php environment and therefore reflects any dynamic changes.

Examples

The following is our standard level 1 template implemented in SSI first and then PHP.

You will notice that the styles, javascript and standard page navigation header use the PHP virtual() function because they contain SSI directives but the files are otherwise unchanged.

Our SSI 'last modified' date 'wheeze' for the footer does not work in a mixed PHP/SSI environment (because you cannot pass variables between PHP and SSI). Instead we have to create a "footer.php" file and set the variable 'real_date' using the PHP date() and getlastmod() functions. This file is invoked with the include() function because it is a standard PHP file. In 'footer.php' we just use 'echo $real_date' to place our last modified date in the output stream. Yes, it's simpler in PHP but we have to maintain two versions of our standard footer. Sigh.

Problems, comments, suggestions, corrections (including broken links) or something to add? Please take the time from a busy life to 'mail us' (at top of screen), the webmaster (below) or info-support at zytrax. You will have a warm inner glow for the rest of the day.