Reading a Remote File Using PHP - Is it good or not?

by Amirol
7 replies
Hi guys... I need your opinion about this topic.

I want to use the code below to include a specific file (ex: terms.php) to all my web pages located on various servers.

$ content = file_get_contents('http://www.google.com/');
if ($ content !== false) {
// my echo here
} else {
// an error happened
}
Case:

Let say I have 3 servers, A, B and C.

Then I have 3 different sites in server A, B and C.

Each sites will have same navigation at the end of the page which I will create a navigation.php file and place it on server A.

Then, I will use those code to get the navigation.php content from server A to appear on all of my web pages (in server A, B and C).

So, what I do want to know is this method is really effective? Does this method will affect my web pages loading time?

I used to use ssi method but I want to know is this method is more efective than ssi?

The reason of using this method is, I want to easily change some of the content especially for the terms/navigation/offers in all my webpages.

Thank you for your reply.

-A.Z
#file #good #php #reading #remote
  • Profile picture of the author Dan Grossman
    Nothing wrong with this, but I highly suggest building a caching mechanism so that the remote request doesn't happen on every page load. It both slows down response time and wastes your bandwidth.

    Easy cache system is to create a text file to store the cached document, and if it's older than X minutes, download the page again and write it back to the file. Otherwise, just read the contents of the local file, which is essentially instant.

    filemtime() gives you the modification time of the file.
    Signature
    Improvely: Built to track, test and optimize your marketing.

    {{ DiscussionBoard.errors[192175].message }}
    • Profile picture of the author vikas1234
      Banned
      [DELETED]
      {{ DiscussionBoard.errors[195228].message }}
      • Profile picture of the author Amirol
        Thanks guys!

        I'll test it out.
        Signature

        Need help with PHP and Laravel?

        {{ DiscussionBoard.errors[195397].message }}
        • Profile picture of the author gdclarke
          Be careful how you set the URL in your PHP code. If this is a variable, you'll need to make sure the proper precautions.

          Graham
          {{ DiscussionBoard.errors[195994].message }}
  • Profile picture of the author ForumJoiner
    If I understand well, you want to include a remote php file. This is not possible, because you'll include the parsed file and not the original one.
    That is, if you try:
    $ content = file_get_contents('http://server.a/test.php');
    you'll not include the test.php, but the parsed test.php, which is totally different.

    The only exception is if test.php is actually a static file, in which case you don't need the php extension.
    {{ DiscussionBoard.errors[195990].message }}
    • Profile picture of the author Dan Grossman
      Originally Posted by ForumJoiner View Post

      If I understand well, you want to include a remote php file. This is not possible, because you'll include the parsed file and not the original one.
      That is, if you try:
      $ content = file_get_contents('http://server.a/test.php');
      you'll not include the test.php, but the parsed test.php, which is totally different.

      The only exception is if test.php is actually a static file, in which case you don't need the php extension.
      He said it was navigation, HTML links, so the parsed output is all he needs. As long as navigation.php is self-contained, no it does not have to be a static file at all.
      Signature
      Improvely: Built to track, test and optimize your marketing.

      {{ DiscussionBoard.errors[196035].message }}
  • Profile picture of the author ForumJoiner
    Each sites will have same navigation at the end of the page which I will create a navigation.php file and place it on server A.
    Well, I understand from here that the navigation.php will take care of the navigation links. If the navigation links are different for each server (like pagination, for instance), the content of navigation.php will be dynamic. If he wants only static, then I agree entirely with you.
    {{ DiscussionBoard.errors[196053].message }}
  • Profile picture of the author Amirol
    Hi guys,

    Thank you for your response.

    I just use include method instead of file_get_contents. (I feel my pages load a bit slow using file_get_contents).

    Anyway, thanks again!
    Signature

    Need help with PHP and Laravel?

    {{ DiscussionBoard.errors[233667].message }}

Trending Topics