1

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

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:

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.)

3 Answers 3

2

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
 ?>
Sign up to request clarification or add additional context in comments.

4 Comments

But if the value of id is not passed in the url how can we use $_GET['id']; ? also the value "2" I used as just an arbitrary example to represent a value determined by user navigation.
If your request goes to www.website.com/dir/index.php?display=page5&id=2, as you wrote, the two available $_GET variables will be $_GET['display'] and $_GET['id']. So the ID will be passed inside your snippet. Every file, that gets included there, can also access the $_GET variables, so the display5.php can access it. The if($id == 2) { was just an example of me for showing off how to deal with the ID. You could do some database SELECT statements here instead, e.g.
Ok I understand. But now, for whatever reason when I try what you explained it doesnt work even though with the url as in the example i cant even $id = $_GET['id']; echo $id;
Ack. There was a typo. It works now thank you very much for the help!
1

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.

Comments

1

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!"); } ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.