【问题标题】:How to correctly check if page was included? [duplicate]如何正确检查页面是否包含? [复制]
【发布时间】:2014-12-25 22:13:30
【问题描述】:

我想确保我的页面被包含在工作索引页面中。我想知道什么是确保我的页面被包含而不是自己呈现的正确方法?

现在我正在检查是否至少有 2 个包含的文件,但我不确定我是否会这样做。 包括('config/config.inc.php');

$cms = new cms();

if(($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'POST') && !empty($_GET['page'])) {
    include($cms->GetTheme() . "/head.php");
    $cms->IncludeModule($_GET['page']); <- actual page being included
    include($cms->GetTheme() . "/foot.php");
} // end (GET || POST) && GET
else { // just index.php
    include($cms->GetTheme() . "/head.php");
    foreach($cms->GetModuleList() as $module) {
        echo " <a href=\"index.php?page=$module\"> $module </a><br />";
    }
    include($cms->GetTheme() . "/foot.php");
} // end ELSE

包含的页面以及我如何检查它是否包含

<?php
    $module_name        = 'Log out user';
    $module_directory   = 'admin';
    $this->SetTitle($module_name); // setting page title

    if(count(get_required_files()) < 2) {
        header('Location: index.php');
    }
    else {
        if(isset($_SESSION['user'])) {
            $this->DestroyUser();

            echo "You have been logged out! Please navigate to the <a href=\"index.php?page=login\">Login Page</a>.";
        }
        else {
            header('Location: index.php?page=login'); 
        }
    }
?>

【问题讨论】:

  • 如果您将要包含的文件移出网络根目录,您就会知道它们永远不会被自己请求。

标签: php include


【解决方案1】:

我不确定你是否在谈论这个:

include 'test.php';

如果是,那么做一个这样的简单测试:

test.php

$testVar = '1';

index.php

include 'test.php';
echo $testVar;

我不知道你用的是什么库,所以我希望这个简单的例子能让你理解。

【讨论】:

    猜你喜欢
    • 2011-07-15
    • 1970-01-01
    • 2012-03-17
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 2018-11-16
    相关资源
    最近更新 更多