Home » Php » URL querystring with a php include

URL querystring with a php include

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m trying to include a file to output in a tab on a page. The file itself will pull up just fine, but when I try to add the required querystring to it, it gives me a “failed to open stream: No such file or directory” error.

I’ve tried just a straight include and tried setting the querystring as a variable. Here’s where I’m at right now.

$listingVars = '?mls=' . $_REQUEST['mlid'] . '&lid=0&v=agent';include("agentview.php$listingVars");

Has anyone successfully done this?

How to&Answers:

You can’t include a query string in an include().

Assuming this is a local script, you could use:

$_REQUEST['mls'] = $_REQUEST['mlid'];
$_REQUEST['lid'] = 0;
$_REQUEST['v'] = 'agent';
include("agentview.php");

if it’s a remote script on a different server, don’t use include.

Answer:

I created a variable on the 2nd page – and passed it a value on the first page – and it worked for me:

*Page with include: 'index.php'
 <?php $type= 'simple'; include('includes/contactform.php'); ?>


*Page included: 'includes/contactform.php'

 switch($type){
  case 'simple':
   //Do something simple
  break;

  default:
   //Do something else
  break;
 }

Answer:

I modify the accepted answer given by Frank Farmer a bit to work for different queries:

Include twice would cause problem:

$_REQUEST['mls'] = $_REQUEST['mlid'];
$_REQUEST['lid'] = 0;
$_REQUEST['v'] = 'agent';
include("agentview.php");

//changing the v to another
$_REQUEST['v'] = 'agent2';
include("agentview.php");

For those who encounter this multiple include problem, you could wrap you code inside “agentview.php” in a function:

Inside agentview.php

function abc($mls,$lid,$v){
   ...your original codes here...
}

file need to call agentview.php

include_once("agentview.php");
abc($_REQUEST['mlid'], 0, 'agent');
abc($_REQUEST['mlid'], 0, 'agent2');

Hope it helps someone encounter the same problem like me and thanks Frank Farmer for the great solution which saved me alot of time.