PHP include() with GET attributes (include file.php?q=1)(PHP include() 带有 GET 属性(包含 file.php?q=1))
问题描述
我想include()
一个位于我的服务器上的 php 文件,带有额外的 GET 属性.但它不起作用:
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');
它给出的错误:
PHP Warning: include(): Failed opening './search.php?q=1' for inclusion
似乎它试图打开一个字面名为search.php?q=1"的文件,而不是打开search.php"文件并向其发送 GET 属性.
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.
*请注意,如果我不放置任何 GET 属性,它确实有效:
*Note that it does work if I don't put any GET attributes:
include('search.php');
推荐答案
您不想这样做:您必须执行 http 请求才能传递 GET 参数.您以这种方式调用的 PHP 脚本将在单独的 PHP 进程中运行.
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.
最佳方式是在本地包含文件:
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
或者,更简洁地,将 search.php
中的任何内容放入可以使用参数调用的函数或类中:
or, more cleanly, putting whatever you have in search.php
into a function or class that you can call with a parameter:
include('search.php'); // defines function my_search($q)
my_search(1);
这篇关于PHP include() 带有 GET 属性(包含 file.php?q=1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP include() 带有 GET 属性(包含 file.php?q=1)


基础教程推荐
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01