11

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

NightHawk
  • 3,633
  • 8
  • 37
  • 56
  • 2
    I can answer the "it's just blank" problem. Normally `require()` would trigger an error for non-existant files (because the parameter isn't treated as "URL" but as raw filename). It seems your `error_reporting()` is *completely* disabled. Shutting off notices is okay if you're proficient, but you should definitely leave E_ERRORs on (development environment). – mario Dec 20 '10 at 00:17
  • @mario You are correct that error_reporting() is turned off and I just didn't realize it. Thank you! – NightHawk Dec 20 '10 at 00:22

4 Answers4

19

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.

s3v3n
  • 8,203
  • 5
  • 42
  • 56
1

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.

Codemwnci
  • 54,176
  • 10
  • 96
  • 129
0

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

Breezer
  • 10,410
  • 6
  • 29
  • 50
  • What are you talking about? He's not using $_GET at all. – s3v3n Dec 19 '10 at 23:59
  • well i'm assuming he is in the page.php code and if i would in the page that requires that code put at the end of the url var=somethingelse it would change the behaviour of the page.php page – Breezer Dec 20 '10 at 00:01
  • 1
    require does not make a new request, so it does not touch any $_GET. it just includes that code, like a local fopen(). no requests, no GETs, no POSTs. – s3v3n Dec 20 '10 at 00:01
  • and that's what i'm saying it only includes the code and if setting any get variables would be pointless and even if it were possible it could get overriden by the user, since it's simple including the code in the current one – Breezer Dec 20 '10 at 00:04
  • if i were vague in describing what i mean my apologies – Breezer Dec 20 '10 at 00:05
0

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.

Rudie
  • 52,220
  • 42
  • 131
  • 173