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
$id = $_GET['id'];
$display = $_GET['display'];
$displays = array('page1', 'page2', 'page3', 'page4', 'page5&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:
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'
However when i go to this page:
I get the error message "Page not found. Return to Index". (The table row id with value of 2 does exist in the database.)
You should better handle the display and ID values separately. 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:
<?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
!
PHP separates them from the query string. When you use in_array()
, you'll check whether page5
is in $displays
, as you can see from your code, it's not.
I suggest you use var_dump($_GET);
and then look at the source of the produced HTML to see how GET parameters are being handled.
Thats why in $_GET["display"]
stands only "page5" and in $_GET["id"]
the 2. You can check the ID in the page5.php.
For example in page5:
<? if (empty($_GET["id"]) || !is_numeric($_GET["id"])) { die("ID isn't a number!"); } ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.