防止 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>

注意:除非出于学习目的,否则不要使用这种方法,它很糟糕,可能很难维护,并且可能最终会很慢,因为我有很多服务器端方法可以在客户端执行。

短跑

您阅读页面内容并回显它!不要那样做。使用 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>";

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在 PHP 中对字符串 "/\/\" 使用 str_replace

来自分类Dev

php防止和在字符串中创建代码

来自分类Dev

PHP:str_replace 從字符串的末尾

来自分类Dev

PHP str_replace不替换字符“°”

来自分类Dev

php str_replace用于丹麦字符

来自分类Dev

PHP str_replace用'-'替换&字符

来自分类Dev

PHP str_replace替换不匹配的字符串中的文本

来自分类Dev

str_replace在我的PHP字符串中不起作用

来自分类Dev

如何使用str_replace替换PHP中的多个字符串?

来自分类Dev

为什么在PHP中使用str_replace不能将hash(#)从字符串中删除?

来自分类Dev

在PHP中结合substr_replace和str_replace

来自分类Dev

PHP 5.2中的字符“ <”和“>”出现str_replace问题

来自分类Dev

如何用str_replace或preg_replace的php字符串替换单词

来自分类Dev

如何处理文本字符串中的PHP代码,以防止将其视为文本?

来自分类Dev

PHP str_replace无法使用特殊字符

来自分类Dev

在“ str_replace” PHP函数中转义特殊字符

来自分类Dev

PHP凯撒密码功能str_replace更改字符错误

来自分类Dev

使用 PHP 和 str_replace 整理类似 JSON 的字符串

来自分类Dev

在PHP中跟随字符串的Str_replace替代方法

来自分类Dev

PHP使用str_replace将数字更改为字符串而不混淆

来自分类Dev

str_replace替换多个字符串-PHP&Laravel

来自分类Dev

多个str_replace到一个字符串?的PHP

来自分类Dev

str_replace php一个字符串

来自分类Dev

给定输出中的多个 str_replace (PHP)

来自分类Dev

str_replace 方法在 php 中不起作用

来自分类Dev

如何在PHP中str_replace \符号

来自分类Dev

PHP的str_replace与通配符?

来自分类Dev

PHP str_replace“和”

来自分类Dev

防止PHP中的“无代码” foreach循环

Related 相关文章

热门标签

归档