-3

can you tell me what is wrong with this code:

require( 'prepareOrder.php?oID='.$orders["id"].'');

Where: prepareOrder.php is a php located on the webpage root folder and orders[id] is an id from an sql database.

8
  • This looks more like PHP than anything to do with PHP. Anyway, require() cannot accept GET variables as part of the path. Commented Mar 17, 2016 at 16:32
  • 3
    Do you have a file on disk which is named prepareOrder.php?oID=42? Didn't think so... Commented Mar 17, 2016 at 16:34
  • You can acces super globals let say $_GET $_POST in file as you are access in the main file ;) Commented Mar 17, 2016 at 16:35
  • @itzmukeshy7 Except this isn't a super-global, it's $orders. Commented Mar 17, 2016 at 16:36
  • @BenM if you have a look over require( 'prepareOrder.php?oID='.$orders["id"].''); @Levi is trying to get oID through $_GET in prepareOrder.php right or not? ;) Commented Mar 17, 2016 at 16:39

4 Answers 4

1

In PHP, require() is only getting the file name. This means if you require('somefile.php?somevar=somevalue'); this will check if a file called somefile.php?somevar=somevalue exists. I "guess" you dont have this file with that name :) So, if you need to pass parameters to an included file, you just need to declare those parameters before including the file. Example: If you need to include somefile.php and use $somevar inside it, this is how to do it:

$somevar = 'somevalue';
include('somefile.php');

or

require('somefile.php');

and then you can use $somevar inside somefile.php without any problem :)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you all for your answers!
What i wanted to achieve, is after the php has finished it-s operations, to open a new page, called prepareOrder.php and add the id of the order in the link.
0

First of all no such file exists in your system having ? in name

All the variables declared before file include/require are available in that file and variables declared in included/required file are also available in parent file.

So simply include file and access variables as you access in other files ;)

Comments

0

You can't pass parameters when requiring or including a file. It is not executed as a web request, but simply included in the source - imagine copy and pasting the source code contents of the file, not the end result.

See the manual page for include().

Comments

0

PHP's require() function does not accept GET parameters as part of the request path.

As a result, PHP is looking for a file which is physically named prepareOrder.php?oID=1 (assuming $orders["id"] = 1)

You should, however, be able to access the $orders array inside of your prepareOrder.php file, so you can utilise the id key within your required file.

1 Comment

Thank you BenM, i have included the id variable in the prepareOrder.php file, but i have an if statement that is not passing in this way: if (!isset($_REQUEST["oID"]) || !is_numeric($_REQUEST["oID"])) { die("Invalid Order. Please check the order access link!"); }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.