PHP include() with GET attributes (include file.ph

2019-01-17 15:24发布

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');

标签: php include get
7条回答
Fickle 薄情
2楼-- · 2019-01-17 16:11

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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-17 16:14

It works fine!! Thank you

Set the var before the "include" statement

$q=1;
include("search.php");
查看更多
等我变得足够好
4楼-- · 2019-01-17 16:17

Try rewrite $_GET variable.

$oldget=$_GET;
$_GET=array('q'=>'1');
include('search.php');
$_GET=$oldget;
查看更多
放我归山
5楼-- · 2019-01-17 16:21

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

$_GET['q'] = 1;
include('search.php);
查看更多
仙女界的扛把子
6楼-- · 2019-01-17 16:23

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>
查看更多
Summer. ? 凉城
7楼-- · 2019-01-17 16:25

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.

查看更多
登录 后发表回答