简体   繁体   English

从SSI包含PHP的正确方法是什么

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

I'm working on a site which uses a number of SSI's. 我正在使用多个SSI的网站上工作。 Since these are static it makes sense to serve the pages as .shtml and not convert to php includes and serve as .php . 由于这些是静态的,因此将页面作为.shtml而不是转换为php include并作为.php是有意义的。 The problem is now that in one of the includes I'd like to dynamically generate some content with php. 现在的问题是,在其中一个包含项中,我想使用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. 我读过这应该行不通,但我读到的地方是指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. 在我家里的服务器上,我正在运行Apache2。[something]可以正常运行,也可以在将要托管站点的服务器上使用。 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? 那么,这是不是在1.3中不起作用,而在2中却起作用,还是我只是因为某种原因而幸运了,真的应该以另一种方式来做到这一点?

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(?). 我要生成的内容只是在网站的导航中添加了一个CSS类/ id,以突出显示当前页面,因此PHP似乎有些过大,因为在首次生成后它几乎是静态的,因此我可以缓存我想在服务器上输出PHP,但是首先可能有更好的方法(?)。

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. 由于我目前不在该网站上使用PHP进行其他任何操作,因此我将继续使用它。 It is as outlined by Dan M here . 正如Dan M在此概述的那样。

Since these are static it makes sense to serve the pages as .shtml and not convert to php includes and serve as .php 由于这些是静态的,因此将页面作为.shtml而不是转换为php include而作为.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. 好吧,由于在两种情况下都调用PHP引擎,并且在两种情况下都需要解析整个页面,因此将其作为.php页面可能更有意义,因为可以确保它可以在Apache 1.x服务器上正常工作,因为好。 Then you can just output the cached page as .html as you suggested and have that extension take precedence in DirectoryIndex . 然后,您可以按照建议的那样将缓存的页面输出为.html,并且该扩展名在DirectoryIndex具有优先权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM