我想 include()
一个位于我的服务器上的 php 文件,带有额外的 GET 属性。
但它不会工作:
include('search.php?q=1');
它给出的错误:
PHP Warning: include(): Failed opening './search.php?q=1' for inclusion
它似乎试图打开一个名为“search.php?q=1”的文件,而不是打开“search.php”文件并向其发送 GET 属性。
*请注意,如果我不放置任何 GET 属性,它会起作用:
include('search.php');
最佳答案
您不想这样做:您必须执行 http 请求才能传递 GET 参数。您以这种方式调用的 PHP 脚本将在单独的 PHP 进程中运行。
最佳方法是在本地包含文件:
include('search.php');
并手动将任何参数传递给它,例如
$q = "1";
include('search.php'); // expects `$q` parameter
或者,更简洁地,将您在 search.php
中的所有内容放入可以使用参数调用的函数或类中:
include('search.php'); // defines function my_search($q)
my_search(1);
关于具有 GET 属性的 PHP include() (include file.php?q=1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5675550/