当我在浏览器中显示源代码时显示 PHP 代码

标签 php

我尝试使用模板来制作多语言网站,因此我创建了一个名为 languages 的文件夹,其中包含 en.php 和 fr.php 这是 en.php 的代码:

<?php
class Index {

    public $filename;
    public $assigned_vars = array();

    public function set($key, $value) {
        $this -> assigned_vars[$key] = $value;
    }

    public function display() {
        if (file_exists($this -> filename)) {
            $output = file_get_contents($this -> filename);
            foreach ($this->assigned_vars as $key => $value) {
                $output = preg_replace('/{' . $key . '}/', $value, $output);
            }
            echo $output;
        } else {
            echo "*** Missing template ***";
        }
    }

}

$index = new Index;
$index -> filename = "templates/index1.php";
$index -> set('page_title', "Welcome");
$index -> set('first_name', "Welcome to our website.");
$index -> display();
?>

这是 fr.php 的代码:

<?php
class Index {

    public $filename;
    public $assigned_vars = array();

    public function set($key, $value) {
        $this -> assigned_vars[$key] = $value;
    }

    public function display() {
        if (file_exists($this -> filename)) {
            $output = file_get_contents($this -> filename);
            foreach ($this->assigned_vars as $key => $value) {
                $output = preg_replace('/{' . $key . '}/', $value, $output);
            }
            echo $output;
        } else {
            echo "*** Disparus modèle ***";
        }
    }

}

$index = new Index;
$index -> filename = "templates/index1.php";
$index -> set('page_title', "Bienvenue");
$index -> set('first_name', "Bienvenue sur notre site.");
$index -> display();
?>

然后我创建了一个名为 templates 的文件夹并创建了一个名为 index1.php 的文件

这里是index1.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>{page_title}</title>
    </head>
    <body>
        {first_name}
        <br/>
        <a href="?lang=en-us">english</a>
        <a href ="?lang=fr">French</a><br />
        <?php
        echo strftime("Y", time());
        ?>
    </body>
</html>

然后我创建了常规的index.php 这是index.php的代码:

<?php
if (isset($_GET['lang'])) {
    setcookie('language_test', $_GET['lang'], time() + (60 * 60 * 24 * 7));
    if ($_GET['lang'] == "fr") {
        require ('languages/fr.php');
    } else {
        require ('languages/en.php');
    }
} else {
    require ('languages/en.php');
}
?>

这是我的问题,当我尝试在浏览器中显示index.php时,它工作正常,但index1.php中的php代码没有显示任何strftime();,但是当我尝试查看源代码时这 索引.php 它向我展示了这个:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Welcome</title>
    </head>
    <body>
        Welcome to our website.
        <br/>
        <a href="?lang=en-us">english</a>

        <a href ="?lang=fr">French</a><br />
        <?php
        echo strftime("Y", time());
        ?>
    </body>
</html>

提前致谢。很抱歉这么长,也很抱歉我糟糕的英语。

最佳答案

当您调用 file_get_contents($this -> filename); 时,它只是将 strftime("Y", time()); 部分转换为字符串。所以它不执行。

index1.php 中,您应该使用 {time} 而不是

<?php
echo strftime("Y", time());
?>

所以它看起来像

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>{page_title}</title>
</head>
<body>
    {first_name}
    <br/>
    <a href="?lang=en-us">english</a>
    <a href ="?lang=fr">French</a><br />
    {time}
</body>

现在在 en.phpfr.php 中添加时间,如下所示

$index -> set('time', strftime("%Y", time()));

最好不要重新发明模板库。有很多可用的。请参阅此博文 top 25 php template engines

关于当我在浏览器中显示源代码时显示 PHP 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9342451/

相关文章:

php - 克隆实例并链接方法调用,可能吗?

php - 编辑 php.ini 文件

php - 使用 php 在输入字段中插入值

php - 哪个 JavaScript 函数与 PHP urldecode 函数兼容?

php - 在 PAYPAL PHP REST API SDK 中使用对象

javascript - 将 javascript 绑定(bind)到提交按钮时遇到问题

php - Guzzle send() 方法导致 cURL 错误 35 打开的文件太多

php - 语法错误 PHP 5.3.3 - 意外的 '['

php - 索引规范版本页面的正确方法是什么?

php - 为什么在尝试插入英镑值(9000英镑)时未知字符会添加到mysql表中的值中