0

我有一个现有的 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 代码时,页面似乎将大于符号之后的所有内容都解释为文本。当我发布该问题时,此论坛上的某个人建议更改 .htaccess 文件以包含 PHP 解析器。一阵子。我将 .htaccess 文件更改为如下所示:

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

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

4

1 回答 1

0

在您的 PHP 脚本中,您可以调用函数来处理您的 SSI

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

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

于 2014-11-06T13:17:02.383 回答