用 PHP 编写的 SSI 解析器?

发布于 2024-10-16 17:33:57 字数 637 浏览 2 评论 0原文

好吧,这可能听起来有点疯狂,但请耐心听我一分钟。

我正在开发一个网站,其标准是使用 SSI 来包含页眉、页脚和菜单。包含的文件使用 SSI 条件来处理不同的浏览器,一些 #include 嵌套,以及一些 #set / #if 技巧来突出显示菜单中的当前页面。换句话说,它不仅仅是 SSI 中的 #include 指令。

我确信有些人可能会反对美观,但对于静态 HTML 来说,它实际上工作得很好。

现在,问题是:我只想从我的 PHP 脚本中“#include”相同的 SSI 解析的页眉和页脚 html 文件,从而避免代码重复并仍然保持站点的统一外观。如果 PHP 在通常的 mod_php 环境中运行,我就可以通过使用 PHP 的 virtual() 函数来做到这一点。不幸的是,该站点使用 FastCGI/suexec 来运行 PHP(以便每个 VirtualHost 可以作为不同的用户运行),这会破坏 virtual()。

我一直在使用一个用 PHP 编写的相当简单的 SSI 解析器(它处理 #includes 和一些非常简单的 #if 语句),但我想要一个更通用的解决方案。那么,在我发疯并编写一些可能有错误的、更完整的 SSI 解析器之前,有人知道用 PHP 编写的完整 SSI 解析器吗?当然,我也愿意接受在我概述的限制下工作的其他解决方案。

非常感谢您抽出时间。

OK, this might sound a little crazy, but bear with me here for a minute.

I'm working on a site where the standard is to use SSI to include page headers, footers, and menus. The included files use SSI conditionals to handle different browsers, some #include nesting, and some #set / #if trickery to highlight the current page in the menu. In other words, it's more than just #include directives in the SSI.

I'm sure some might argue with the aesthetics, but it actually works quite nicely, for static HTML.

Now, the problem: I'd like to just "#include" the same SSI-parsed header and footer html files from my PHP scripts, thus avoiding code duplication and still maintaining the site's uniform look. If PHP were running in the usual mod_php environment, I'd be able to do just that by using PHP's virtual() function. Unfortunately, the site is using FastCGI/suexec to run PHP (so that each VirtualHost can run as a different user), and this breaks virtual().

I've been using a fairly simple SSI parser I wrote in PHP (it handles #includes, and some really simple #if statements), but I'd like a more general solution. So, before I go nuts and write some probably-buggy, more complete SSI parser, does anyone know of a complete SSI parser written in PHP? Naturally, I'm also open to other solutions that work under the constraints I've outlined.

Thanks so much for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晒暮凉 2024-10-23 17:33:57

看看 ESI :http://en.wikipedia.org/wiki/Edge_Side_Includes

你可以创建一个 PHP 代理来处理它们,它是 Symfony2 中的 HttpCache : https://github.com/fabpot/symfony/blob/master/src/Symfony/Component/HttpKernel/HttpCache/Esi.php

或者使用像 Varnish 这样的 HTTP 代理,性能比交响乐2...

Take a look at ESI : http://en.wikipedia.org/wiki/Edge_Side_Includes

You can create a PHP-proxy to handle them, it's the HttpCache in Symfony2 : https://github.com/fabpot/symfony/blob/master/src/Symfony/Component/HttpKernel/HttpCache/Esi.php

Or use a HTTP proxy like Varnish, more performant than Symfony2...

清引 2024-10-23 17:33:57

我意识到这是一个老问题,但几年前我遇到了同样的问题,尽管是使用 perl 实现。我继续进行之前的尝试,并在实现完整的 apache (2.2.22) mod_include 模拟器/解析器作为 perl 模块 http://search.cpan.org/dist/CGI-apacheSSI/lib/CGI/apacheSSI.pm 很快之后,我找到了 apache 输出过滤器,并意识到这是一个多么完美的解决方案,能够满足我的需求。基本上,您可以告诉 apache 解析脚本的输出,就像它是 .shtml 或 .php (或其他)文件一样。因此,您可以从 perl 或 php(或其他)脚本输出 SSI 标记,并让 apache 解析它。这就是你可以做到的方法(在你的 .htaccess 文件中):

AddOutputFilter INCLUDES .cgi

这适用于普通的 cgi 文件,但要注意,这会给正在执行的所有 .cgi 文件增加相当多的开销,所以我实际上做的是创建一个特殊的扩展名,这样它作为一个 cgi 运行,然后对其输出进行解析,而不会将开销添加到正常的 cgi 文件中:

<Files ~ ".pcgi$">
    Options +SymLinksIfOwnerMatch +Includes
    AddOutputFilter INCLUDES .pcgi
</Files>

对于 php,您可以执行类似的操作:

<Files ~ ".pphp$">
    Options +SymLinksIfOwnerMatch +Includes
    AddOutputFilter INCLUDES .pphp
</Files>

这应该可以解决问题!希望能帮助那里的人。

I realize this is an old question, but I ran into that same problem a few years ago, though with a perl implementation. I went ahead and forked a previous attempt and got pretty far into implementing a full apache (2.2.22) mod_include emulator/parser as a perl module http://search.cpan.org/dist/CGI-apacheSSI/lib/CGI/apacheSSI.pm Soon after that I found apache output filters, and realized how perfect a solution that is for my needs. Basically, you can tell apache to parse the output of your script as if it was a .shtml or .php (or whatever) file. So you can output SSI markup from a perl or php (or whatever) script, and have apache parse that. This is how you can do it (in your .htaccess file):

AddOutputFilter INCLUDES .cgi

That's for normal cgi files, but beware, this adds quite a bit of overhead to all .cgi files being executed, so what I actually do is create a special extension so that it runs as a cgi that then has its output parsed, without having the overhead added to normal cgi files:

<Files ~ ".pcgi$">
    Options +SymLinksIfOwnerMatch +Includes
    AddOutputFilter INCLUDES .pcgi
</Files>

for php you could just do something like:

<Files ~ ".pphp$">
    Options +SymLinksIfOwnerMatch +Includes
    AddOutputFilter INCLUDES .pphp
</Files>

and that should do the trick! Hope that helps someone out there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文