Search code examples
phpssi

What's the correct way to include PHP from an SSI


I'm working on a site which uses a number of SSI's. Since these are static it makes sense to serve the pages as .shtml and not convert to php includes and serve as .php. The problem is now that in one of the includes I'd like to dynamically generate some content with php. Is the following the correct way to go about it or are there other/better solutions. What I'm doing at the moment is:

<!--test.shtml-->
<!DOCTYPE html>
<html lang="en" dir="ltr">
<body>
<!--#include virtual="/test.php" -->
</body>
</html>

<!--test.php-->
<?php echo 'Hello from php'; ?>

I've read that this shouldn't work but where I read that, it was referring to Apache 1.3. On my server at home I'm running Apache 2.[something] and it works fine, it also works on the server where the site will be hosted. So is this something that didn't work in 1.3 but does in 2 or am I just getting lucky somehow and really should be doing it a different way?

The content I'm generating is just adding a css class/id to the navigation on the site, to highlight the current page, so PHP seems a little overkill since it's pretty much going to be static after it's first generated, I can cache the PHP output on the server I suppose but perhaps there's a better way of doing this in the first place(?).

Thanks for your help.

EDIT - Not an answer, but in relation to my alternative method question. I found a perhaps more suitable (I hesitate to say cleaner, since I'm sure that can be argued 'til the cows come home) method for my situation. Since I am not currently using PHP for anything else on the site, I'll stick with it for now. It is as outlined by Dan M here.


Solution

  • Since these are static it makes sense to serve the pages as .shtml and not convert to php includes and serve as .php

    Well, since you're invoking the PHP engine in both cases and the entire page needs to be parsed in both cases doing it as a .php page may make more sense as you're guaranteed it will work on Apache 1.x servers as well. Then you can just output the cached page as .html as you suggested and have that extension take precedence in DirectoryIndex.