Include Once ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thedoc
Forum Newbie
Posts: 2
Joined: Wed May 27, 2009 5:20 pm

Include Once ?

Post 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
Last edited by Benjamin on Fri May 29, 2009 10:20 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Include Once ?

Post 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.
Last edited by McInfo on Tue Jun 15, 2010 4:34 pm, edited 1 time in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Include Once ?

Post 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.
Last edited by McInfo on Tue Jun 15, 2010 4:35 pm, edited 1 time in total.
thedoc
Forum Newbie
Posts: 2
Joined: Wed May 27, 2009 5:20 pm

Re: Include Once ?

Post by thedoc »

Thanks for you reply Mcinfo.

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

Have a great day!
Post Reply