简体   繁体   English

php动态页面

[英]php Dynamic page

I'm not sure if the title here is appropriate and my explanation might be just as bad but here it goes... I am using the following code to generate web pages: 我不确定这里的标题是否合适,我的解释可能同样糟糕但在这里......我使用以下代码生成网页:
source: Dynamic inclusion in PHP 来源: 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

?>

a typical page: 一个典型的页面:

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

My problem is this: I want to add a page to the array of allowed pages that has a dynamic value. 我的问题是:我想将页面添加到具有动态值的允许页面数组中。 You can see my attempt at this in the code above where i added: 'page5&id=$id' 您可以在我上面的代码中看到我对此的尝试:'page5&id = $ id'

However when i go to this page: 但是,当我转到此页面时:

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

I get the error message "Page not found. Return to Index". 我收到错误消息“找不到页面。返回索引”。 (The table row id with value of 2 does exist in the database.) (数据库中存在值为2的表行id。)

You should better handle the display and ID values separately. 您应该更好地分别处理显示和ID值。 For example like this: 例如这样:

<?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

?>

and in display5.php: 并在display5.php中:

 <?php

 // some other initializations

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

 // show more html
 ?>

Just like you will receive a value in $_GET['display'] , you will receive another in $_GET['id'] with a value of 2 ! 就像你将在$_GET['display']收到一个值一样,你将在$_GET['id']收到另一个值为2的值!

PHP separates them from the query string. PHP将它们与查询字符串分开。 When you use in_array() , you'll check whether page5 is in $displays , as you can see from your code, it's not. 当你使用in_array() ,你将检查page5是否在$displays ,正如你从代码中看到的那样,它不是。

I suggest you use var_dump($_GET); 我建议你使用var_dump($_GET); and then look at the source of the produced HTML to see how GET parameters are being handled. 然后查看生成的HTML的源代码,了解如何处理GET参数。

Thats why in $_GET["display"] stands only "page5" and in $_GET["id"] the 2. You can check the ID in the page5.php. 这就是为什么$_GET["display"]只代表“page5”而$_GET["id"]代表2.你可以查看page5.php中的ID。

For example in page5: 例如在第5页:

<? if (empty($_GET["id"]) || !is_numeric($_GET["id"])) { die("ID isn't a number!"); } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM