Forum Moderators: coopster

Message Too Old, No Replies

Including a remote file

Can this be done.

         

mack

4:49 pm on Feb 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Ok quite a complexed situation but I will try to explain.

I have a page. The page is dynamic with pretty much infinate variables. On the page there is an iframe that contains a php page.

The php page is there to deliver content based on the page title of the main page (the page with the iframe)

The php script reads the main page title then tries to include a file with the page title as a query string.

Here is some simple code I used...


<?php
$url = $HTTP_REFERER;
$file = fopen ("$url", "r");
if (!$file) {
echo "<p>Can't grab title.\n";
exit;
}
while (!feof ($file)) {
$line = fgets ($file, 1024);
if (eregi ("<title>(.*)</title>", $line, $out)) {
$title = $out[1];
break;
}
}
fclose($file);
$q = urlencode ($title);
include "http://www.example.com/folder/file.php?keywords=$q";
?>

When I view main page i get the following error within the iframe...


Warning: Failed opening 'http://www.example/com/folder/file.php?keywords=foo' for inclusion (include_path='.:/usr/share/php') in /home/site/public_html/ad.php on line 17

In this example it passed "foo" as the keyword because it was the main page title for the page with the iframe.

At first I though php includes where not posible due to my php.ini configuration but I checked and this feature was set.

Any ideas.. Thanks in advance.

Mack.

mincklerstraat

5:25 pm on Feb 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can do remote includes, but when using url wrappers to include remote files, it means that what gets included is the output via the http protocol.

Type the same address into a web browser, look at it, that's what's being included.

This means that the file on the other server shouldn't look like:

<?php
$this = 'that';
somefunc('someparam');
?>

It needs to be:
<?php
echo "
<?php
$this = 'that';
somefunc('someparam');
?>
";
or just put it in a static .txt file and include that.

mack

5:37 pm on Feb 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Thanks for your reply.

To be honest it is the output that I want to include. Does this make things any different?

Mack.

ergophobe

7:33 pm on Feb 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If all you want is the output of the included file and you don't want it to essentially become part of the code of the main script, it sounds like you could just use an iframe or a frame unless I'm misundertanding.

Or do you want to capture the output and process it?

As Minck said, though, you can't pass a GET parameter to an include, since it's not an HTTP request.

So [example.com...]

is really more like

$param = val

include (http://www.example.com/page.php);

Since you may have permission issues and so on, you may want to do something like

clearstatcache();
$file = 'http://www.example.com/page.php';
if (is_file($file) && is_readable($file))
{
include($file);
}