PHP - include() file not working when variables are put in url?

試著忘記壹切 提交于 2019-12-01 04:31:20

问题


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?


回答1:


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.




回答2:


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');



回答3:


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




回答4:


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:


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



来源:https://stackoverflow.com/questions/8301552/php-include-file-not-working-when-variables-are-put-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!