Page 1 of 1
include error only when passing a querystring
Posted: Fri Oct 26, 2007 3:05 pm
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
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]
Posted: Fri Oct 26, 2007 3:10 pm
by RobertGonzalez
Why are you doing that? Just set the var, then include the file.
Posted: Fri Oct 26, 2007 3:16 pm
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.
Posted: Fri Oct 26, 2007 3:18 pm
by RobertGonzalez
Yes, that is exactly what I mean. And how did it not work for you?
Posted: Fri Oct 26, 2007 3:36 pm
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
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.
Posted: Fri Oct 26, 2007 4:42 pm
by RobertGonzalez
page1.php
page2.php
Code: Select all
<?php
include 'page1.php';
echo $var;
?>
Run these. What do you get?
Posted: Fri Oct 26, 2007 5:34 pm
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);
Posted: Sun Oct 28, 2007 10:21 am
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.
Posted: Sun Oct 28, 2007 10:34 am
by feyd
The includes shouldn't have worked in the first place. I suspect they may have used a custom build of PHP.
Posted: Sun Oct 28, 2007 11:18 am
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.
Posted: Sun Oct 28, 2007 11:21 am
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.
Posted: Sun Oct 28, 2007 11:26 am
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
Posted: Sun Oct 28, 2007 11:37 am
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'];
Posted: Sun Oct 28, 2007 4:50 pm
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.