For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > November 2005 > PHP 5: Including a file with a query string generates an error









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author PHP 5: Including a file with a query string generates an error
kmalone75

2005-11-22, 6:56 pm

I am relatively new to PHP, so forgive me if this is a problem that has
been dealt with already.

I am trying to include a file that has a query string appended to the
end of the URL. I am running PHP 4 locally and the included file runs
fine. When I try it on the live server, which is running PHP 5, I am
getting an error like:

Warning: functionName() [function.include]: Failed opening
http://www.URL.com/includedFile.php?name=value' for inclusion
(include_path='.;C:\php5\pear')

There are many other files being included on the page with no query
strings, and they get included as they should. If I try to include a
file with a query string, that is when I get errors on the page.

Has anybody come across problems with using the include() function to a
include a file with a query string?

Oli Filth

2005-11-22, 6:56 pm

kmalone75 said the following on 22/11/2005 18:42:
> I am trying to include a file that has a query string appended to the
> end of the URL. I am running PHP 4 locally and the included file runs
> fine. When I try it on the live server, which is running PHP 5, I am
> getting an error like:
>
> Warning: functionName() [function.include]: Failed opening
> http://www.URL.com/includedFile.php?name=value' for inclusion
> (include_path='.;C:\php5\pear')
>
> There are many other files being included on the page with no query
> strings, and they get included as they should. If I try to include a
> file with a query string, that is when I get errors on the page.
>
> Has anybody come across problems with using the include() function to a
> include a file with a query string?



I think the bigger problem is that you're include()-ing files via HTTP -
do you really want your included files to be processed before the
results are passed to the original PHP file?


--
Oli
kmalone75

2005-11-22, 6:56 pm

Good point. But I was unable to get the page to process using relative
paths. Using relative paths works fine if I am simply including another
php file, but, when I want the included page to do some extra
processing of query string variables, I get nothing if I just use a
relative path, i.e. include("/includes/includedFile.php?name=value").

As I said, I am new to PHP, so it sounds like I may be going about my
include all wrong. Any suggestions for me?

Thanks.

kmalone75

2005-11-22, 6:56 pm

Actually, I do want included files to be processed first. It needs to
process the query string variables and then be included in the main
page that has the include() function.

This example is on php.net

Example 16-7. include() through HTTP

/* This example assumes that www.example.com is configured to parse
..php
* files and not .txt files. Also, 'Works' here means that the
variables
* $foo and $bar are available within the included file. */

// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';

// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';

// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';

$foo = 1;
$bar = 2;
include 'file.txt'; // Works.
include 'file.php'; // Works.

I should be able to include through HTTP, which is what I think I need
to do.

Any suggestions are comments would help.

Oli Filth

2005-11-23, 7:55 am

kmalone75 wrote:
> Actually, I do want included files to be processed first. It needs to
> process the query string variables and then be included in the main
> page that has the include() function.


Including a PHP script via HTTP and using a query string to affect its
behaviour is a very round-about (and potentially insecure) way of doing
this.

Why not declare a function inside your included file, and then call
that function from the main script, passing the variables as arguments?

e.g.:

include.php (make sure this is below the web-root)
===========

<?php
function foo($a, $b, $c)
{
/* ... Do some stuff ... */
return $x;
}
?>

main.php
========

<?php
include "include.php";

foo("Blah", 5, 2.3);

?>

--
Oli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com