Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP Include from External Site

Posted on 2011-05-09
11
Medium Priority
?
387 Views
Last Modified: 2013-11-19
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?
0
Comment
Question by:Les_N00bian
  • 6
  • 5
11 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35722195
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");
0
 

Author Comment

by:Les_N00bian
ID: 35723309
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.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 35724046
This is pretty unambiguous:
failed to open stream: Permission denied

Have a look at the man page for include() here: http://php.net/manual/en/function.include.php

To quote, "When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Supported Protocols and Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script."

What all that means is, "It depends."

To dig into this problem, please install this little script on your server (the one that wants to include() a file from another server).  And then post a link to it here at EE.  We can have a look and probably find out exactly what is awry.  Thanks, ~Ray
<?php phpinfo();

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Les_N00bian
ID: 35724135
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:  URL
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35725009
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.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35757830
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
0
 

Assisted Solution

by:Les_N00bian
Les_N00bian earned 0 total points
ID: 35757889
I never did get it working on that site. It worked because I tried it on a different host. I'm not sure that's terribly helpful to someone who is trying to overcome the error. However I guess my original question was just how to pull an include from another site so this is how I did it:
<?php 
$filename = 'http://your-url.com/file-you-want-to-include.txt';
$handle = fopen($filename, "rb"); 
$contents = fread($handle, 5000);
fclose($handle);
print $contents;
?>  

Open in new window


The the "rb" stands for Read and Binary. Technically only the "r" is necessary but I read somewhere that the "b" will make it compatible across different hosting OS's (Windows/Linux). The 5000 should be any number that is the size of the file you are reading or larger (in kilobytes).
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35758100
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
0
 

Author Comment

by:Les_N00bian
ID: 35758116
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?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35758232
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!
0
 

Author Closing Comment

by:Les_N00bian
ID: 35783843
I found the code needed using other resources
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
By following these Magento e-commerce development tips, you can increase your website's conversion and profitability. Read this post for more details.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question