0

I am confused why my code below don't work:

include('section_common.php?cat=' . $cat_map[$cat]['url']);

it give:

Warning: include(section_common.php?cat=cat): failed to open stream: No error in C:\xampp\htdocs\1.php on line 67

while when i visit localhost/section_common.php?cat=cat it works well, i think that the problem is in the function include itself, as the included link is working in browser and in include it give failed to open stream.

miranda kerry
  • 19
  • 1
  • 7

4 Answers4

0

You can't include files like that with a query string. Perhaps restructuring your code will make your code better and less fragile.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

Is not possible to pass parameters to include or require.

DEFAULT

$cat= $cat_map[$cat]['url'];
include("section_common.php");

In "section_common.php":

echo $cat;

SESSION

session_start();
$cat= $cat_map[$cat]['url'];
$_SESSION["cat"] = $cat;
include("section_common.php");

In "section_common.php":

session_start();
echo $_SESSION["cat"];

CURL:

$connecturl = curl_init('http://localhost/section_common.php?cat='.$cat_map[$cat]['url']);
curl_setopt($connecturl,CURLOPT_RETURNTRANSFER,true);
$output = curl_exec($connecturl);
curl_close($connecturl);
echo $output;

Jquery:

$.ajax({
   url: "section_common.php",
   type: "GET",
   data: {cat:"<?=$cat_map[$cat]['url']?>"},
   success: function(data){
      $("body").html(data);
   }
})
0

When you include a file you give a file path, it's not an http request so if you give some parameters like:

include("file.php?id=1");

He will looking for the file who named "file.php?id=1" and don't do what you are expecting.

To do what you wanna do just set a variable in your script to your value and if you include your file after you will be able to access to your variable:

$cat= $cat_map[$cat]['url'];

include("section_common.php"); // you can use $cat in this script

It's because "include();" just copy paste the file included. But you have to be carfull if you include this script in another place because maybe he won't find $cat.

Edgarth
  • 13
  • 1
  • 6
0

You should supply a file name for include(), but what you've written down is not. If you have some conditional cases to be done in section_common.php, maybe you should pass the parameter as $_SESSION variable.

C. Leung
  • 6,218
  • 3
  • 20
  • 32