PHP include() 带有 GET 属性(包含 file.php?q=1)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5675550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP include() with GET attributes (include file.php?q=1)
提问by anonymous
I want to include()
a php file located on my server, with additional GET attributes.
But it won't work:
我想要include()
一个位于我的服务器上的 php 文件,带有额外的 GET 属性。但它不会工作:
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');
回答by Pekka
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.
您不想这样做:您必须执行 http 请求才能传递 GET 参数。您以这种方式调用的 PHP 脚本将在单独的 PHP 进程中运行。
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:
或者,更简洁地,将您拥有的任何内容search.php
放入您可以使用参数调用的函数或类中:
include('search.php'); // defines function my_search($q)
my_search(1);
回答by Haza
The easysolution is to set your GET value before you include the file.
在简单的解决方法是设置你的GET价值,你包括文件之前。
$_GET['q'] = 1;
include('search.php);
回答by Matteo-SoftNet
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:
实际上,写入$_GET
数组有点脏,没有必要。您可以简单地在您的页面中的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
变量将在您包含的页面中可见,并且可以(显然)在任何包含页面中进行不同的设置。
回答by Dador
Try rewrite $_GET variable.
尝试重写 $_GET 变量。
$oldget=$_GET;
$_GET=array('q'=>'1');
include('search.php');
$_GET=$oldget;
回答by zero cool
It works fine!! Thank you
它工作正常!!谢谢
Set the var before the "include" statement
在“include”语句之前设置var
$q=1;
include("search.php");
回答by Jamie M
If like me you needed this to be permanently in the 'include' link it can be done with jQuery.
如果像我一样,您需要将其永久保存在“包含”链接中,则可以使用 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 的浏览器。
回答by David van Diepen
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>