21

I want to include() a php file located on my server, with additional GET attributes. But it won't work:

include('search.php?q=1');

The error it gives:

PHP Warning:  include(): Failed opening './search.php?q=1' for inclusion

Seems like it tries to open a file literally named 'search.php?q=1' instead of opening the 'search.php' file and sending it the GET attributes.

*Note that it does work if I don't put any GET attributes:

include('search.php');
João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
anonymous
  • 211
  • 1
  • 2
  • 3
  • 5
    It can not work. You can't use query parameters there. – Eliasdx Apr 15 '11 at 10:46
  • *(reference)* http://de.php.net/manual/en/function.include.php – Gordon Apr 15 '11 at 10:47
  • What makes you think it should work? GET is an HTTP method, i.e. it used by the HTTP protocol, which is handled by your web server. `include` on the other hand, just reads and interprets the file directly from the file system, no web server involved. These are two different processes. – Felix Kling Apr 15 '11 at 10:49
  • 1
    @Felix `include` can use URIs if `allow_url_include` and `allow_url_fopen` is enabled. – Gordon Apr 15 '11 at 10:51
  • @Gordon: Ok, yes I didn't think about that. But then you would have to pass an absolute URI I assume. – Felix Kling Apr 15 '11 at 10:53
  • 1
    @Felix yes of course and it's disabled on most shared hosters for security reasons anyways – Gordon Apr 15 '11 at 10:55
  • possible duplicate of [PHP - include a php file and also send query parameters](http://stackoverflow.com/questions/1232097/php-include-a-php-file-and-also-send-query-parameters) – nalply Jun 01 '14 at 12:43

7 Answers7

28

You don't want to do this: You'd have to do a http request to be able to pass GET parameters. A PHP script you call in this way will run in a separate PHP process.

The optimal way is to include the file locally:

include('search.php');

and to pass any parameters to it manually, like e.g.

$q = "1";
include('search.php');  // expects `$q` parameter

or, more cleanly, putting whatever you have in search.php into a function or class that you can call with a parameter:

include('search.php');  // defines function my_search($q)  
my_search(1);       
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 2
    Or even better use a class structure. `include('search.php'); $search = new Search($q);` – Jonathon Apr 27 '13 at 16:20
  • @JonathonWisnoski because throwing in some class(es) just makes everything magically better somehow right? right? – PeeHaa Jun 30 '14 at 23:32
  • Yes, encapsulation is important. Sharing global variables between files is just asking for incredibly hard to diagnose bugs and generally unmaintainable code. – Jonathon Jul 01 '14 at 03:11
23

The easy solution is to set your GET value before you include the file.

$_GET['q'] = 1;
include('search.php);
Haza
  • 2,991
  • 1
  • 16
  • 13
4

Try rewrite $_GET variable.

$oldget=$_GET;
$_GET=array('q'=>'1');
include('search.php');
$_GET=$oldget;
Dador
  • 5,277
  • 1
  • 19
  • 23
2

It works fine!! Thank you

Set the var before the "include" statement

$q=1;
include("search.php");
zero cool
  • 21
  • 2
2

In effect, writing into the $_GET array is a bit dirty and not necessary. You could simply set a variable in your page before the include statement:

$q=1;
include("search.php");

this way, the $q variable will be visible in your included page and can (obviously) be set differently in any including page.

Matteo-SoftNet
  • 531
  • 3
  • 10
1

maybe an other option, although it comes with its own limitations, using AJAX-injection into a div.. most simple way to to this is with jQuery:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>
<script>$('#box').load('search.php?q=1');</script>

<div ID="box"></div>
0

If like me you needed this to be permanently in the 'include' link it can be done with jQuery.

The solution I used recently was as follows:

First, declare the location of the div to be populated by the included file:

<div id="imgdiv"> </div>

Then add a jQuery call to populate it, but insert your GET variables with PHP:

<script>                
window.onload = function(){     
        $("#imgdiv").load('php/show-img.php?id=<?php echo $i; ?>&name=<?php echo $name; ?>');           
}
</script>

Hopefully that will be sufficient to get it working, assuming a java enabled browser is used of course.

Jamie M
  • 422
  • 3
  • 8