Link to home
Start Free TrialLog in
Avatar of Les_N00bian
Les_N00bian

asked on

PHP Include from External Site

I manage a few different sites for the same company. The company has offices located in different cities and states, so each one has it's own site. We have a disclaimer that appears at the bottom of all pages in all sites which is the same for all however. Every once in a while this disclaimer requires tweaking and it can be a pain to do this across all sites. To make things easier, I'm currently using an iFrame to pull a single disclaimer for all. This presents two problems however. 1) I'm afraid I might be negatively impacting the SEO of the sites, and 2) the iFrame doesn't retain the style of the site it is on. I'm trying to switch to a PHP include to replace the iFrames but am getting "failed to open stream: Permission denied" errors. Is it possible to pull an include from an external site?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Yes, it is possible, but it is usually controlled by the hosting environment.

You might try this to see if it works:

echo file_get_contents("path/to/included/file");
Avatar of Les_N00bian
Les_N00bian

ASKER

It doesn't look like include works for an external file. However I discovered fopen and I'm experimenting with that. I can get it to work on local files but I get this error when I try to pull a txt...

Warning: fopen(http:/my-site-2.com/myfile.txt) [function.fopen]: failed to open stream: Permission denied in /home/www/my-site-1.com/include_test.php on line 13

Open in new window


Where "my-site-1.com" is the site I'm trying to pull the file to and "my-site-2.com" is the site I'm trying to pull the .txt file from.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I did get the fopen function to work from a different domain, so it does appear that you are correct, it's something to do with the PHP settings on that particular host. I would still like to know what the issue is with that host. Here is the URL you requested:  User generated image
I cannot see a problem in the PHPInfo() output.  Suggest you set up very simple tests - perhaps only a line or two of text - and see if you can get to those minimal files.  If so, then you can build up the programming and test as you build.  If it fails at a certain build point you will have a good clue about what might be wrong.
Instead of deleting this question, please post the solution that you found so others can benefit from it.  If your solution was different from anything I posted here, you should accept your own solution.  If the comments here guided you toward the solution, you might want to accept more than one answer and split the points.  Thanks, ~Ray
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Interesting.  I think the include() function and what you read from the code example above would produce different results.  For example, if you include() a script that contains a PHP command, the command will be executed (on whatever server it lives on) and the resulting variables will be injected into your namespace or variable scope.  In contrast, if you fread() a script that contains a PHP command, the PHP command itself, in clear text, (not the results) will arrive in the $contents variable and then will be printed by that script.

In any case, if you've got a solution that works in your application that is a good thing and thank you for posting the solution.  Best regards, ~Ray
I'll have to try that out. The text file I used for my site included HTML which was rendered (not presented in clear text). I guess it depends on where the file being opened is rendered, at the server or the browser level, yes?
HTML is rendered by the browser.  PHP is interpreted (and maybe produces browser output) on the server side, then the output is sent to the browser.  Different layers of the presentation!
I found the code needed using other resources