同时启用PHP解析器和Include语句


Enabling both a PHP parser and an Include statement

我有一个现有的SHTML页面,其中包含一些菜单的INCLUDE语句。这样做效果很好。到目前为止,我的.htaccess文件看起来像:

Options +FollowSymLinks

我的Include声明看起来是这样的:

<!--#include virtual="menu_primary.shtml" -->

我有一个新的需要,添加一些PHP代码到主页。PHP代码查询Mysql数据库以返回一行。如果数据库中存在该行,我希望将其显示在SHTML页面上。如果该行不存在,则不执行任何操作。以下是PHP代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "myusername";
$password = "mypassword";
$dbname = "mydbname";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT notice from notification where page = 'home'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "Message is: " . $row["notice"]. "<br>";
    }
} else {
    echo "";
}
mysqli_close($conn);
?> 
</body>
</html>

当我实现这个PHP代码时,页面似乎将Greater Than符号之后的所有内容都解释为文本。当我发布这个问题时,论坛上有人建议修改.htaccess文件,以包含PHP解析器。有一段时间。我把.htaccess文件改成这样:

Options +FollowSymLinks
<FilesMatch "'.(htm|html|shtm|shtml)$">
    SetHandler application/x-httpd-php5
</FilesMatch>

然而,当我这样做时,PHP代码运行良好,并且我在SHTML页面上显示数据库中的数据,但#Include语句不再工作。如何在同一SHTML页面中同时启用PHP和#Include代码?非常感谢你看这个。

在PHP脚本中,您可以调用虚拟函数来使用SSI

<?php
virtual('menu_primary.shtml');

有一个非常古老的页面更详细地讨论了这一点http://www.zytrax.com/tech/php/php_ssi.htm