Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP include() with GET attributes (include file.php?q=1)

Tags:

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'); 
like image 263
anonymous Avatar asked Apr 15 '11 10:04

anonymous


People also ask

What does include () do in PHP?

PHP Include Files. The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

What are the difference between include () and require () file in PHP?

The require() function will stop the execution of the script when an error occurs. The include() function does not give a fatal error. The include() function is mostly used when the file is not required and the application should continue to execute its process when the file is not found.

Why PHP include is not working?

You need to try the path of functions. php within the system and not its url. Do you have console access? If so just find out what directory is the file in and include it using the full path.


1 Answers

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);        
like image 106
Pekka Avatar answered Dec 03 '22 20:12

Pekka