简体   繁体   English

带有GET属性的PHP include()(包括file.php?q = 1)

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

I want to include() a php file located on my server, with additional GET attributes. 我想在我的服务器上include()一个php文件,带有额外的GET属性。 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. 似乎它试图打开一个名为'search.php?q = 1'的文件而不是打开'search.php'文件并向其发送GET属性。

*Note that it does work if I don't put any GET attributes: *请注意,如果我不添加任何GET属性,它确实有效:

include('search.php');

You don't want to do this: You'd have to do a http request to be able to pass GET parameters. 您不希望这样做:您必须执行http请求才能传递GET参数。 A PHP script you call in this way will run in a separate PHP process. 以这种方式调用的PHP脚本将在单独的PHP进程中运行。

The optimal way is to include the file locally: 最佳方式是在本地包含文件:

include('search.php');

and to pass any parameters to it manually, like eg 并手动传递任何参数,例如

$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: 或者,更清楚地说,将search.php任何search.php放入可以使用参数调用的函数或类中:

include('search.php');  // defines function my_search($q)  
my_search(1);       

The easy solution is to set your GET value before you include the file. 简单的解决方案是在包含文件之前设置GET值。

$_GET['q'] = 1;
include('search.php);

Try rewrite $_GET variable. 尝试重写$ _GET变量。

$oldget=$_GET;
$_GET=array('q'=>'1');
include('search.php');
$_GET=$oldget;

It works fine!! 它工作正常!! Thank you 谢谢

Set the var before the "include" statement 在“include”语句之前设置var

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

In effect, writing into the $_GET array is a bit dirty and not necessary. 实际上,写入$_GET数组有点脏,没有必要。 You could simply set a variable in your page before the include statement: 您只需在include语句之前在页面中设置一个变量:

$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. 这样, $q变量将在您包含的页面中可见,并且(显然)可以在任何包含页面中以不同方式设置。

If like me you needed this to be permanently in the 'include' link it can be done with jQuery. 如果像我一样,你需要将它永久地放在'include'链接中,它可以用jQuery完成。

The solution I used recently was as follows: 我最近使用的解决方案如下:

First, declare the location of the div to be populated by the included file: 首先,声明要包含的文件填充div的位置:

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

Then add a jQuery call to populate it, but insert your GET variables with PHP: 然后添加一个jQuery调用来填充它,但是用PHP插入你的GET变量:

<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. 希望这足以让它工作,假设当然使用支持java的浏览器。

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: 也许是另一种选择,虽然它有自己的局限性,使用AJAX注入div。最简单的方法是使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM