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

标签 php include get

我想 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/

相关文章:

C++循环包含问题

android - 从 URL 获取 HTML 源代码

Python-Facebook fb_dtsg

ruby-on-rails - 获取rails3中不同 Controller Action 的输出

php - 为什么在 PHP 中使用 set_include_path() 以及如何从不同的文件夹自动加载类?

c++ - 在 C/C++ 中#include 头文件

php - 在 WooCommerce 产品上显示自定义前缀以及每米价格

php - .htaccess 密码保护目录但允许图像文件类型

PHP:将当前时间戳插入 SQL Server 2005

php - 如何从使用 sparqllib 完成的查询中获取 SPARQL 的 ASK 答案?