NOTE: READ THIS FIRST
I wrote this response to Arch13's previous post.
I thought he was asking 2 questions:
- Should I include files dynamically?
- Is it possible for included files to use variables set in the URL?
This response tries to tackle those questions. But re-reading his post, I wasn't sure he was asking for this
So that's why I posted follow-up questions (my previous post).
But in an effort to speed things along (and in case he indeed did ask for this reponse), I will now post the original reply I wrote.
See this as more of an informational post, rather than continuing the discussion.
End note
I see your problem. (EDIT: I didn't

)
Though using global variables should work on included pages.
BUT..... why would you?
What does the included page do? If the included page does the same thing everytime (i.e. display an image that the first script can guess itself), why would you include it, and not just build it in.
IMHO including files can have the following reasons:
- clearer code (large included libraries for example)
- seperate authors. No need to include other's code because it makes debugging difficult
- often-changing files (if the included files change often, or are perhaps changed by someone else)
- code in the included files is not always needed. (as seems to be your case. You include files dynamically).
Now the last point is only valid if the different included files (say brewery.php, stable.php, garage.php etc) vary in code
or this difference in functionality is expected in the future
If the code is predictable (as your quoted second file seems to be), why not put it into your main index.php?
Your suggestion:
Quote:
Originally Posted by Arch13
Or would using your suggested page on the index.php page, and simply creating an anchor for id2 in any included page be more effeciant?
|
would then be the way to go.
If, however, you still want / need to variably include files, and hence need to pass the $_REQUEST['id2'] parameter to the include file, you should not need to do much.
On my installation this works out of the box. I just created 2 files (ugly code, but it works

)
Save them in their suggested names, and try it out. One displays the value for 'id' (taken from the URL) and includes the second file. The second file prints the value of id2 which is also taken from the URL. In essence this proves that passing the values you need, works.
First file:
(include_test.php)
PHP Code:
<?php
echo '<pre>';
echo "include test \n";
echo '$_REQUEST[id] equals: '.$_REQUEST['id']." \n";
echo "Including file: \n";
include ('included.php');
echo "passed including files \n";
?>
Second file:
(included.php)
PHP Code:
<?php
echo 'Inluded file thinks $_REQUEST[id2] equals: '.$_REQUEST['id2']."\n";
?>
Now if you call "include_test.php?id=foo&id2=bar" you should see this:
Code:
include test
$_REQUEST[id] equals: foo
Including file:
Inluded file thinks $_REQUEST[id2] equals: bar
passed including files
I hope this clears things up a little in either case. I guess it's obvious I'm not sure why you dynamically include files, so I tried to help by giving that "reasons for including" list above.
Anyhow, unless your setup blocks it, passing URL variables to included files works automatically. (And so should so called 'global variables', so if you need to create a variable, you can "pass" it to included files as well. In fact, that's how my scripts usually load a bunch of settings, it includes a settings file which creates a whole bunch of global variables.)
Note: indeed I forgot two semicolons in my previous code, so it didn't parse. The above code was copy/pasted from the working files so they should work
