include error only when passing a querystring

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
palodau
Forum Newbie
Posts: 7
Joined: Fri Oct 26, 2007 2:52 pm

include error only when passing a querystring

Post by palodau »

Hi everyone,

allow_url_fopen is set to on. I am running PHP version 5.2.4. Here is the problem:

The statement below works fine

Code: Select all

include("includes/test.php");
The statement below gives an error

Code: Select all

include("includes/test.php?var=1");


I have tried a variety of things such as building the querystring. This has not solved my problem.

The error I get is:

Code: Select all

Warning: include(includes/test.php?var=test) [function.include]: failed to open stream: No such file or directory in [path]
Warning: include() [function.include]: Failed opening 'includes/test.php?var=test' for inclusion (include_path='.') in [path]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Why are you doing that? Just set the var, then include the file.
palodau
Forum Newbie
Posts: 7
Joined: Fri Oct 26, 2007 2:52 pm

Post by palodau »

Wow - that was a quick response. Many thanks for the info. Do you mean something as follows?

Code: Select all

$var = "hoot";
include("includes/test.php");
I tried this earlier and still no joy.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yes, that is exactly what I mean. And how did it not work for you?
palodau
Forum Newbie
Posts: 7
Joined: Fri Oct 26, 2007 2:52 pm

Post by palodau »

The calling page has the following code

Code: Select all

$var = "hoot";
include("includes/test.php");
The called page has the following code

Code: Select all

$var = $_GET['var'];
echo $var;
Please tell me I am doing something dumb. The issue has got rather confusing because the actual page I am trying to fix works on a server. We are moving to a new server and now all of a sudden only includes with querystrings are not working. With the code above I get no error (display_errors is on) and no echo. Initially I thought that allow_url_fopen must be off but I have checked and even used ini_set() to make sure it was on.

I am currently comparing ini settings from the two servers to see if anything else comes up. The site is being hosted and the hosting company swear that everything is ok.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

page1.php

Code: Select all

<?php
$var = 'Hello world';
?>
page2.php

Code: Select all

<?php
include 'page1.php';
echo $var;
?>
Run these. What do you get?
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

You seem to be confused by how including and remote including work.

Unless test.php returns php code (which I doubt is the case), it is wrong for you to include it in that way (you need to use a full url, http://.. etc)

Most probably you want to include test.php and pass it some variables. There are several ways to do that, with increasing level of okay-ness:
1. Hacky:

Code: Select all

$_GET['var']=1;
include('includes/test.php');
2. Using a global variable.

Code: Select all

$g_nVar = 1; 
//test.php works with $g_nVar, instead of $_GET['var']
include('includes/test.php');
3. Using a function inside test.php

Code: Select all

include('includes/test.php');
FunctionFromTest(1);
palodau
Forum Newbie
Posts: 7
Joined: Fri Oct 26, 2007 2:52 pm

Post by palodau »

Thanks alot! That works great. I still have a problem however as the site we are moving over to a new server is proliferated with querystring includes such as include(test.php?var=1). They simply don't work on the new server. It seems to be interpreting includes as local file paths rather than URLs. allow_url_fopen is set to on.

We are taking this site over from another company and hundreds of pages use this form of include. Is there any setting that you have to turn on to allow this? It is rather frustrating that it works on the old server and not on the new one.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The includes shouldn't have worked in the first place. I suspect they may have used a custom build of PHP.
palodau
Forum Newbie
Posts: 7
Joined: Fri Oct 26, 2007 2:52 pm

Post by palodau »

Testing on at home I can get the includes to work when allow_url_include is set to 'on'. I have to edit the php.ini file for this. Trying to do it with ini_set is not currently working. I doubt the hosting company will edit their php.ini file just for us! I really don't want to have to start editing all this include statements to use global variables but I might have to.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

allow_url_fopen isn't involved with include() unless it's a remote request. The act of having a query string isn't remote to PHP. There's something else between things.
palodau
Forum Newbie
Posts: 7
Joined: Fri Oct 26, 2007 2:52 pm

Post by palodau »

I was looking now referring to allow_url_include : From php.ini - whether to allow include/require to open URLs (like http:// or ftp://) as files
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Note the results:

Code: Select all

feyd:~ feyd$ php -v
PHP 5.2.3 (cli) (built: Jul 26 2007 17:14:09) 
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

feyd:~ feyd$ php -d allow_url_include=1 -d allow_url_fopen=1 -r "var_dump(ini_get('allow_url_fopen'),ini_get('allow_url_fopen')); include 'test.php?var=crazy';"
string(1) "1"
string(1) "1"
PHP Warning:  include(test.php?var=crazy): failed to open stream: No such file or directory in Command line code on line 1
<span style=color:red>
Warning: include(test.php?var=crazy): failed to open stream: No such file or directory in Command line code on line 1
</span>PHP Warning:  include(): Failed opening 'test.php?var=crazy' for inclusion (include_path='.:/opt/local/lib/php') in Command line code on line 1
<span style=color:red>
Warning: include(): Failed opening 'test.php?var=crazy' for inclusion (include_path='.:/opt/local/lib/php') in Command line code on line 1

feyd:~ feyd$ cat test.php
<?php

echo $_GET['var'];
palodau
Forum Newbie
Posts: 7
Joined: Fri Oct 26, 2007 2:52 pm

Post by palodau »

Thanks for everyones help! I can include("test.php?var=1") to work when allow_url_fopen and allow_url_include are both set to 1. Seems the old site was using PHP 4 which did not have allow_url_include.

I tested this at home. Now all I have to do is see if I can get this set on the hosting server. Might have to try setting this in the .htaccess file or trying to use a local php.ini file because ini_set is not working. Not sure if the hosting company have allow_url_include off for security purposes. Obviously now that I have looked into it I would rather not use querystrings but this is how it was originally coded and I would rather not have to trudge through all the files. I will post how it goes tomorrow. Again many thanks.
Post Reply