Using query string in require_once in PHP

On one of my pages, I have require_once('../path/to/url/page.php'); which works without problems. At that moment, when I add the query string require_once('../path/to/url/page.php?var=test'); , it will no longer include the file. It is just empty. Anyone have any ideas why? Can you use the query string in the request?

Thanks Ryan

+7
source share
4 answers

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 a file called page.php?var=test and include it, because on unix you are allowed to have that file name. If you want to pass a variable to this script, just define it: $var="test" and it will be available for use in the script.

+16
source

requires downloading the file (from the file path) to include. It does not request this file via apache (or another web server), so you cannot pass query strings this way.

If you need to transfer data to a file, you can simply define a standard php variable.

Example

 <?php $a_variable = "data"; require_once('../path/to/url/page.php'); ?> 

Please note: the variable must be set before calling include / require, otherwise it will not be available.

+1
source

you just need to take paths, it would be pointless to add any request, since it doesn’t do any, it just includes the required code in the current

0
source

All confirm true. But the most important thing: since $_GET is global, it is present in all the files included in it, so there is absolutely no need to pass these parameters using include.

0
source

All Articles