-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.

BenM
  • 52,573
  • 26
  • 113
  • 168
Levi
  • 1
  • 1
  • This looks more like PHP than anything to do with PHP. Anyway, `require()` cannot accept `GET` variables as part of the path. – BenM Mar 17 '16 at 16:32
  • 3
    Do you have a ***file on disk*** which is named `prepareOrder.php?oID=42`? Didn't think so... – deceze Mar 17 '16 at 16:34
  • You can acces super globals let say `$_GET $_POST` in file as you are access in the main file ;) – itzmukeshy7 Mar 17 '16 at 16:35
  • @itzmukeshy7 Except this isn't a super-global, it's `$orders`. – BenM Mar 17 '16 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? ;) – itzmukeshy7 Mar 17 '16 at 16:39
  • @itzmukeshy7 True, but the value the OP is assigning to the `oID` `GET` param isn't itself a super-global, so he won't be able to access it via `$_GET` inside of `prepareOrder.php`... – BenM Mar 17 '16 at 16:42
  • You mean `$_GET` is not a superglobal in PHP? ;) – itzmukeshy7 Mar 17 '16 at 16:44
  • @itzmukeshy7 No, that's not what I'm saying at all. – BenM Mar 17 '16 at 22:06

4 Answers4

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 :)

Oussama
  • 595
  • 5
  • 16
  • Thank you all for your answers! – Levi Mar 17 '16 at 20:26
  • 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. – Levi Mar 17 '16 at 20:40
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 ;)

itzmukeshy7
  • 2,669
  • 1
  • 21
  • 29
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().

Dezza
  • 1,094
  • 4
  • 22
  • 25
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.

BenM
  • 52,573
  • 26
  • 113
  • 168
  • 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!"); } – Levi Mar 18 '16 at 08:31