简体   繁体   中英

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

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?

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

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.

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM