6

I would like to require a file but also pass GET variables through the url, but when I write:

<?php
   require_once("myfile.php?name=savagewood");
?>

I get a fatal error. How would I accomplish this functionality in a different way, such that I don't get a fatal error?

Kara
  • 6,115
  • 16
  • 50
  • 57
rolling_codes
  • 15,174
  • 22
  • 76
  • 112

4 Answers4

20

variables will be available as normal you do not have to pass like this.

$name='savagewood';
require_once("myfile.php");

$name will be available in myfile.php

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
1
<?php
$getVarsArray = $_GET;
$postVarsArray = $_POST;
/* n number of variables and lines of code*/
include('script-a.php');
?>

Now in script-a.php has access to $getVarsArray and $postVarsArray and if in any case you are in doubt you can use $GLOBALS to access any variable throughout the life cycle of a script. But using global variables is a sin. :)

Kumar
  • 5,038
  • 7
  • 39
  • 51
  • 1
    `$_GET` and `$_POST` are *[superglobals](http://php.net/manual/en/language.variables.predefined.php)*, meaning that they exist in every scope. Copying the GPC superglobals into another variable like this is *beyond silly* and borderline insane. Don't do that. – Charles Apr 24 '11 at 17:48
  • 1
    @Charles, using those GET and POST variables w/out treating them will be equally _beyond silly_ and borderline insane. I never do that. The **sane** will treat those superglobals & will copy them and unset POST/GET vars. Or a paranoid may even write his own implementation for using GET/POST var. This being, not in the scope of the question, wasn't mentioned. My bad. – Kumar Apr 25 '11 at 04:07
0

It is not necessary to pass the variables to the new file, because by including the new file the variables are maintained. Remember that $ _GET is an array, and it can be modified within the script.

<?php
   $_GET['name'] = "savagewood";
   require_once("myfile.php");
?>

On this case, $_GET['name'] is accesible from the "myfile.php"

-2

I think I have got a perfect solution to your problem. You can use implode function of PHP. But I would strongly recommend doing Shakti Singh's code.

SOLUTION CODE

echo implode(file('http://path-to-your-site/your-dir/myfile.php?name=savagewood'));
mario
  • 144,265
  • 20
  • 237
  • 291
Jack Billy
  • 7,141
  • 6
  • 27
  • 38