PHP - 当变量放入 url 时,include() 文件不起作用?

发布于 2024-12-18 11:04:10 字数 349 浏览 1 评论 0原文

在 PHP 中,我构建了一个网页,它使用 include() 来加载网站的某些部分。 然而,我现在遇到了一个类似的问题: 当我使用如下网址时:data-openov-storingen.php?type=actueel 它给了我这个错误:

Warning: include(data-storingen.php?type=actueel): failed to open stream: No such file or directory in blablabla

是否可以在 include() url 中传递 get 变量?

In PHP I've build a webpage that uses include() for loading parts of the website.
However, I now ran into something like a problem:
When I use an url like: data-openov-storingen.php?type=actueel
It gives me this error:

Warning: include(data-storingen.php?type=actueel): failed to open stream: No such file or directory in blablabla

Is it even possible to pass get variables in an include() url?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟柳画桥 2024-12-25 11:04:10

这种方式的 include 不会获取 URL,而是从文件系统中获取文件,因此不存在查询字符串之类的东西。

不过,你可以这样做:

$_GET['type'] = 'actueel';
include('data-storingen.php');

include in this way doesn't fetch a URL, it fetches a file from the filesystem, so there's no such thing as a query string.

You can do this, though:

$_GET['type'] = 'actueel';
include('data-storingen.php');
蔚蓝源自深海 2024-12-25 11:04:10

include() 函数不通过 HTTP 访问文件,而是通过操作系统自己的文件系统访问文件。所以 GET 变量不被计算在内。 (因为它们不是文件名的一部分)。

通俗地说,包含的意义是将一个文件上的所有内容“复制/粘贴”到另一个文件上,这样您就不会拥有一个巨大的文件,而是一些较小的、更易于维护的文件。

The include() function does not access the file via HTTP, it accesses the file through the OS's own file system. So GET variables are not counted. (as they are not part of the file name).

In layman's terms, the point of include is to "copy/paste" all the contents on one file to another on the file, so that you don't have one gigantic file, but a few smaller, more maintainable ones.

俯瞰星空 2024-12-25 11:04:10

除非您有完全限定的 URL,并且地址中包含 protocol://,否则 PHP 会将您传递到 include()/require() 的内容解释为本地文件,这意味着它正在寻找您驱动器上的一个文件,其真实名称是 data-storingen.php?type=actueel,而您只有 data-storingen.php

由于您正在处理本地文件,因此不支持查询字符串,并且您必须将其从传递给 include() 的“文件名”中删除。

Unless you've got a fully qualified URL, with protocol:// in the address, PHP will interpret what you pass into include()/require() as a LOCAL file, which means it's looking for a file on your drive whose name real is data-storingen.php?type=actueel, whereas you've only got data-storingen.php.

Since you're dealing with a local file, there is no support for query strings, and you have to strip that out of the "filename" you're passing to include().

伏妖词 2024-12-25 11:04:10

您可以/应该始终在外部设置变量,因为您无法通过 URL 执行此操作...

$_GET['type'] = "actueel";
include("data-storingen.php");

然后包含的文件可以访问变量(假设您使用 $_GET['type']在包含的文件中)

You could/should always set the variables outside, since you can't do this via the URL....

$_GET['type'] = "actueel";
include("data-storingen.php");

Then the included file can access the variables (assuming you use $_GET['type'] in the included file)

泛泛之交 2024-12-25 11:04:10

不,不可能传递这样的变量。不过,您可以在 data-storingen.php 内部使用变量 actueel,只要它在您要包含它的页面中的 include 语句之前声明即可。

将包含视为将包含文件中的代码复制粘贴到当前文件中。所以你可以有一个文件:

$actueel = 'abc';
include(data-storingen.php);

然后你的 data-storingen.php:

echo $actueel;

它会输出'abc'。

No it is not possible to pass variables like that. You can use the variable actueel inside of data-storingen.php though, as long as it is declared in the page you are including it from, before the include statement.

Think of including as copy-pasting the code from the included file into the current file. So you can have a file:

$actueel = 'abc';
include(data-storingen.php);

And then your in data-storingen.php:

echo $actueel;

And it will output 'abc'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文