Page 1 of 1

Include Once ?

Posted: Wed May 27, 2009 5:25 pm
by thedoc
Greetings all, I'm a newcomer to the forum, however been with PHP for a while and have come across this problem. The code returns the following errors:

Code: Select all

<?php
...
include_once("page/add.php?id=".$id);
...
?>
Warning: include_once(page/add.php?id=3) [function.include-once]: failed to open stream: No such file or directory in /asset/function.php on line 18

Warning: include_once() [function.include]: Failed opening 'page/add.php?id=3' for inclusion (include_path='.:/usr/local/php5/lib/php') in asset/function.php on line 18
The thing is when I remove everything past the '?' everything works as it should. Can I not use the question mark with an include, and if not can someone suggest a better way of going about this?

Many thanks,

Doc

Re: Include Once ?

Posted: Wed May 27, 2009 6:37 pm
by McInfo

Code: Select all

<?php
$id = 3;
include_once("page/add.php?id=".$id);
?>
tries to open a file named "add.php?id=3". This is not a valid file name, hence the error.

The include_once statement and its siblings are control structures like if and else except that they are used to include code from other files on the filesystem. They do not (usually) issue HTTP requests.

This does work:

origin.php

Code: Select all

<?php
$id = 3;
include_once 'add.php';
?>
add.php

Code: Select all

<?php
echo $id;
?>
And it has the same effect as:

origin2.php

Code: Select all

<?php
$id = 3;
echo $id;
?>
Edit: This post was recovered from search engine cache.

Re: Include Once ?

Posted: Wed May 27, 2009 7:16 pm
by McInfo
You should use the method I demonstrated in my last post, but I thought I would share an exception to "include_once does not issue HTTP requests".

php.ini

Code: Select all

allow_url_include = On
origin3.php

Code: Select all

<?php
include_once 'http://127.0.0.1/get.php?echo=hello';
 
// If your server requires authentication:
//include_once 'http://username:password@127.0.0.1/get.php?echo=hello';
?>
get.php

Code: Select all

<?php
echo $_GET['echo'];
?>
Edit: This post was recovered from search engine cache.

Re: Include Once ?

Posted: Thu May 28, 2009 5:31 am
by thedoc
Thanks for you reply Mcinfo.

That was a case of me being suitably thick headed there! Many thanks!

Have a great day!