问题
On one of my pages I have a require_once('../path/to/url/page.php');
which works with no problems. The moment I add a query string require_once('../path/to/url/page.php?var=test');
it won't include the file anymore. It's just blank. Anyone have any ideas of why? Can you not use a query-string in a require?
Thanks, Ryan
回答1:
By using require_once('../path/to/url/page.php?var=test');
, php will not make a new request to page.php, it will actually search for the file named page.php?var=test
and include it, because in unix, you are allowed to have such a filename. If you want to pass a variable to that script, just define it: $var="test"
and it will be available for use in that script.
回答2:
require loads a File (from a file path) to include. It does not request that file through apache (or other webserver), therefore you cannot pass query strings in this way.
If you need to pass data into the file, you can simply define a standard php variable.
Example
<?php $a_variable = "data"; require_once('../path/to/url/page.php'); ?>
Note, the variable must be set before the include/require is called, otherwise it won't be available.
回答3:
All answes true. But most importantly: since $_GET
is a global, it's present' in all included files as well, so there's absolutely no use in passing those parameters with the include.
回答4:
require only accepts paths it would be pointless to add any request since it doesn't make any it simple includes the required code into the current one
来源:https://stackoverflow.com/questions/4485845/using-a-query-string-in-require-once-in-php