阻止str_replace评论字符串中的php代码

时间:2017-05-08 10:56:58

标签: php html

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

因为它接收了所有请求并使用str_replace返回页面,所以它应该正常工作(因为它正在使Web模板正常工作)但问题是我不能拥有php作为模板一部分的文件内的代码,仅在index.php。

所以我的问题是,有没有办法阻止str_replace将PHP代码转换为注释?

的index.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>

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

1 个答案:

答案 0 :(得分:1)

您阅读了页面内容并回复它!不要那样做。请改用include('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>";

但通常这是糟糕的编程习惯。正如之前的评论所建议的那样,请使用模板引擎。