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

2
  • 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, 2010 at 0:17
  • @mario You are correct that error_reporting() is turned off and I just didn't realize it. Thank you!
    – NightHawk
    Dec 20, 2010 at 0:22

4 Answers 4

20

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.

7
  • 1
    Or define $_GET['var'] = 'test';
    – Rudie
    Dec 19, 2010 at 23:59
  • 3
    There's no need in this, actually this can bring trouble.
    – s3v3n
    Dec 20, 2010 at 0:05
  • @Rudie: I second s3v3n, definitely don't do this.
    – Jon
    Dec 20, 2010 at 0:09
  • "php will not make a new request to page.php, it will actually search for the file named page.php?var=test" -- that makes sense and explains the fact that the include failed. Thanks!
    – NightHawk
    Dec 20, 2010 at 0:23
  • Can you elaborate why $_GET['var'] = 'test'; will bring trouble? My problem is that $_GET... works, but $var="test" doesn't. Sep 27, 2013 at 0:00
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.

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.

1
  • 1
    There's also no need to push that variable into $_GET. You can simply define it.
    – s3v3n
    Dec 20, 2010 at 0:03
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

5
  • What are you talking about? He's not using $_GET at all.
    – s3v3n
    Dec 19, 2010 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, 2010 at 0: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, 2010 at 0: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, 2010 at 0:04
  • if i were vague in describing what i mean my apologies
    – Breezer
    Dec 20, 2010 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.