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