防止str_replace注释字符串中的 php 代码



所以我正在尝试创建一个网站,我使用一个php文件作为索引(index.php(,几乎是控制整个网站的页面。

由于它接收所有请求并使用str_replace返回页面,因此它都正常工作(例如,它使Web模板正常工作(,但问题是我不能在作为模板一部分的文件中使用php代码,只能在index.php中。

所以我的问题是,有没有办法防止str_replace将php代码变成注释?

索引.php:

<?php
//dirs
$pagesDir = "pages/";
$templatesDir = "templates/";
$errorsDir = "errors/";
if (isset($_REQUEST['page'])) {
    if ($_REQUEST['page'] != "")
        if (file_exists($pagesDir . $_REQUEST['page'] . ".html"))
            $page_content = file_get_contents($pagesDir . $_REQUEST['page'] . ".html");
    else
        if (file_exists($_REQUEST['page'] . ".html"))
            $page_content = file_get_contents($_REQUEST['pages'] . ".html");
            else
                echo "<h1>Page:" . $_REQUEST['page'] . " does not exist!       Please check the url and try again!</h1>";
} else {
    $page_content = file_get_contents($pagesDir . "home.html");
}
//PLACEHOLDER REPLACEMENT
$page_content = str_replace("!!HEAD!!", file_get_contents($templatesDir .    "head.html"), $page_content);
$page_content = str_replace("!!BODY!!", file_get_contents($templatesDir . "body.html"), $page_content);
$page_content = str_replace("!!FOOT!!", file_get_contents($templatesDir . "eofScripts.html"), $page_content);
//RETURN THE CONTENT OF THE PAGE
echo $page_content;

更改后的新调度程序(这个有效(:

<?php
$templatesDir = "templates/";
$pagesDir = "pages/";
$loggedPagesDir = "templates/logged";
$pageExists = false;
$pageContent = null;
require_once('scripts/php/db_conn.php');
if (isset($_REQUEST['page'])) {
    $page = $_REQUEST['page'] . ".php";
}
if (isset($_SESSION['redirect_reason'])) {
    $dialogs->alertDialog("warningDialog", $_SESSION['redirect_reason']);
    unset($_SESSION['redirect_reason']);
}
if (isset($_SESSION['user_action'])) {
    $dialogs->alertDialog("infoDialog", $_SESSION['user_action']);
    unset($_SESSION['user_action']);
}
if ($user->is_logged()) { //Only runs beyond this point if user is logged, if not, it will run the other one.
    if (isset($_POST['logout_btn'])) {
        $user->logout();
        $user->redirect("pageDispatcher.php");
    }
    if (isset($page)) {
        if ($page != "") {
            if (file_exists($pagesDir . $page)) {
                $pageExists = true;
                $pageContent = ($pagesDir . $page);
            } else {
                echo "<h1>Page: " . $page . "does not exist! Please check the url and try again</h1>";
            }
        } else {
            $pageExists = true;
            $pageContent = ($pagesDir . "loggedhome.php");
        }
    } else {
        $pageExists = true;
        $pageContent = ($pagesDir . "loggedhome.php");
    }
} else { //Only runs beyond this point if user isn't logged.
    if (isset($_POST['login_btn'])) {
        if ($user->login($_POST['email'], $_POST['password']) == false) {
            $dialogs->loginFailed();
        } else {
            $_SESSION['user_action'] = "Welcome back " . $_SESSION['user_name'];
            $user->redirect("pageDispatcher.php");
        }
    }
    if (isset($page)) {
        if ($page != "") {
            if (file_exists($pagesDir . $page)) {
                $pageExists = true;
                $pageContent = ($pagesDir . $page);
            } else {
                echo "<h1>Page: " . $page . " does not exist! Please check the url and try again!</h1>";
            }
        } else {
            $pageExists = true;
            $pageContent = ($pagesDir . "home.php");
        }
    } else {
        $pageExists = true;
        $pageContent = ($pagesDir . "home.php");
    }
}
?>
<html>
<?php include($templatesDir . "head.html"); ?>

<body>
<?php
if ($user->is_logged()) {
    include($templatesDir . "loggedBody.html");
} else {
    include($templatesDir . "body.html");
}
include($pageContent);
?>
</body>
</html>

注意:除非出于学习目的,否则不要使用此方法,它不好,可能很难维护,并且最终可能会很慢,因为我有很多服务器端方法可以做客户端的事情。

您阅读页面的内容并回显它!别这样。请改用包含('file.html'(。只是为了解释,(如果你必须(这样做:

$pages=['head.html','body.html','eofScripts.html'];
$page=$_REQUEST['page'];
if(in_array($page,$pages)) include($page);
else echo "<h1>Page: $page does not exist!</h1>";

但通常这是糟糕的编程实践。如评论中的建议,请使用模板引擎。

最新更新