PHP - include() file not working when variables ar

2019-02-18 19:29发布

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?

5条回答
甜甜的少女心
2楼-- · 2019-02-18 19:44

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');
查看更多
Root(大扎)
3楼-- · 2019-02-18 19:47

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().

查看更多
ゆ 、 Hurt°
4楼-- · 2019-02-18 20:00

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)

查看更多
孤傲高冷的网名
5楼-- · 2019-02-18 20:02

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'.

查看更多
混吃等死
6楼-- · 2019-02-18 20:05

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.

查看更多
登录 后发表回答