PHP - 当变量放入 url 时,include() 文件不起作用?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这种方式的
include
不会获取 URL,而是从文件系统中获取文件,因此不存在查询字符串之类的东西。不过,你可以这样做:
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:
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.
除非您有完全限定的 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 isdata-storingen.php?type=actueel
, whereas you've only gotdata-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().
您可以/应该始终在外部设置变量,因为您无法通过 URL 执行此操作...
然后包含的文件可以访问变量(假设您使用
$_GET['type']
在包含的文件中)You could/should always set the variables outside, since you can't do this via the URL....
Then the included file can access the variables (assuming you use
$_GET['type']
in the included file)不,不可能传递这样的变量。不过,您可以在
data-storingen.php
内部使用变量actueel
,只要它在您要包含它的页面中的 include 语句之前声明即可。将包含视为将包含文件中的代码复制粘贴到当前文件中。所以你可以有一个文件:
然后你的
data-storingen.php
:它会输出'abc'。
No it is not possible to pass variables like that. You can use the variable
actueel
inside ofdata-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:
And then your in
data-storingen.php
:And it will output 'abc'.