4

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?

3
  • 5
    1. If you read the error, you'll understand what your problem is. So don't bla bla it. 2. The preview below the textarea where you typed your question is there for a reason: Help us help you (by making your question properly formatted and clear) Commented Nov 28, 2011 at 20:01
  • 1
    I've edited the question for you. Please format your questions properly next time. With 90 rep you'd expect one to know how to edit a question. Commented Nov 28, 2011 at 20:12
  • @Nacereddine: Indeed, I did know what was going wrong, but that was the reason I was asking about this. I wanted to know more about the details. Commented Nov 28, 2011 at 20:20

5 Answers 5

9

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');
Sign up to request clarification or add additional context in comments.

1 Comment

What the... does that work too? So now you're just setting the get variable? Haha nice :D
6

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.

6 Comments

Thansk for the info, I'll rewrite that piece of code then, thanks!
However, It does not copy/paste like you say. I know that, because I have a double include (an included file , which has an include in it) and the double included file can't access a function from the main file... EDIT: my bad, It can access that function.
an included file is treated like a function. It can access variables from the outside (global), but the outer, including file cannot access the variables inside (much like an application cannot access function scoped variables).
Huh? The outer file can access everything from the inner file after the file has been included.
Haha, indeed, I actually wanted to comment on that, because that is the whole reason I'm including files XD Ah well. I got the info I wanted, thanks guys :)
|
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().

Comments

2

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)

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.