Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a query-string in require_once in PHP

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

like image 525
NightHawk Avatar asked Dec 19 '10 23:12

NightHawk


2 Answers

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.

like image 98
s3v3n Avatar answered Oct 04 '22 15:10

s3v3n


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.

like image 29
Codemwnci Avatar answered Oct 04 '22 16:10

Codemwnci