php的几个页面包括ID

时间:2014-08-20 16:58:44

标签: php

包含两到三页并通过ID包含它们的正确方法是什么,例如: index.php?page = one 将包含 one.html index.php?page = two 将包含 two.html 到同一个php页面?

3 个答案:

答案 0 :(得分:1)

虽然之前的两个答案都容易受到文件包含攻击,但我建议使用预定义的可用页面数组来实现不同的方法:

<?php

    $pages = array('one'=>'one.php',
                   'two'=>'two.php');

    if(in_array($_GET['page'], array_keys($pages))) {
        // only include if the page is really defined in your $pages array
        include($pages[$_GET['page']]);
    } else {
        // don't include or simply include a default page here
        // include('default.php');
    }

?>

这是你想要的吗?

答案 1 :(得分:0)

编辑:鉴于以下不可否认的观点,设置一系列允许的网页也是一个好主意。

<强> URI:

index.php?pages=one,two,three

<强>脚本:

$allowedPages = array('one', 'two', 'three');

if($_GET['pages']){
    foreach(explode(',', $_GET['pages']) as $page){
        if(file_exists($page.'.html') && in_array($page, $allowedPages)){
            include_once($page.'.html');
        }
    }
}

或URI中的数组,保持page查询字符串变量为单数:

<强> URI:

index.php?page[]=one&page[]=two&page[]=three

<强>脚本:

$allowedPages = array('one', 'two', 'three');

if($_GET['page']){
    foreach($_GET['page'] as $page){
        if(file_exists($page.'.html') && in_array($page, $allowedPages)){
            include_once($page.'.html');
        }
    }
}

答案 2 :(得分:-1)

您必须检查文件是否存在以及$_GET['page']是否为有效值

    <?php
    $pageName = $_GET['page'] . '.php';
    if(isset($pageName) && $pageName != '')
    {
        if(file_exists($pageName))
        {
            require_once($pageName);
        }
    }
    ?>
相关问题