1

我不确定这里的标题是否合适,我的解释可能同样糟糕,但它就在这里......我正在使用以下代码生成网页:
来源:PHP 中的动态包含

<?php

$id = $_GET['id']; 

$display = $_GET['display'];
    $displays = array('page1', 'page2', 'page3', 'page4', 'page5&amp;id=$id');

if (!empty($display)) {
        if(in_array($display,$displays)) {
            $display .= '.php';
            include($display);
        }
        else {
        echo 'Page not found. Return to
        <a href="index.php">Index</a>';
        }
    }
    else { //show html

?>

一个典型的页面:

www.website.com/dir/index.php?display=page4

我的问题是:我想将一个页面添加到具有动态值的允许页面数组中。你可以在上面我添加的代码中看到我的尝试:'page5&id=$id'

但是,当我转到此页面时:

www.website.com/dir/index.php?display=page5&id=2

我收到错误消息“找不到页面。返回索引”。(数据库中确实存在值为 2 的表行 id。)

4

3 回答 3

2

您应该更好地分别处理 display 和 ID 值。例如像这样:

<?php

$display = $_GET['display'];
$displays = array('page1', 'page2', 'page3', 'page4', 'page5');

if (!empty($display)) {
        if(in_array($display,$displays)) {
            $display .= '.php';
            include($display);
        }
        else {
        echo 'Page not found. Return to
        <a href="index.php">Index</a>';
        }
    }
    else { //show html

?>

在 display5.php 中:

 <?php

 // some other initializations

 $id = $_GET['id'];
 if($id == 2) { // make some special actions for id = 2

 // show more html
 ?>
于 2013-01-26T14:31:32.997 回答
1

就像您将收到一个价值 in 一样$_GET['display'],您将收到另一个$_GET['id']价值为 的in 2

PHP 将它们与查询字符串分开。当你使用 时in_array(),你会检查是否page5是 in $displays,从你的代码中可以看出,它不是。

我建议您使用var_dump($_GET);然后查看生成的 HTML 的源代码,以了解如何处理 GET 参数。

于 2013-01-26T14:24:26.303 回答
1

这就是为什么 in$_GET["display"]只代表“page5”和$_GET["id"]2。您可以在 page5.php 中检查 ID。

例如在第 5 页中:

<? if (empty($_GET["id"]) || !is_numeric($_GET["id"])) { die("ID isn't a number!"); } ?>
于 2013-01-26T14:24:46.760 回答