Jump to content

include another servers file?


sgt_fireman

Recommended Posts

Hey guys, So I need to try and create an equivalent of doing,

 

<? include ("http://www.example.com/example.php")?>

 

Now I know this isn't possible to just use the code above (At least I've been told)

 

So does anyone know any alternatives? Thanks very much in advanced guys.

Link to comment
Share on other sites

Do you want to execute the PHP code in that file, or just get the resulting HTML?

 

If it's the former: You cannot. At least not without editing the script to print out the actual PHP code, or changing it to a pure text file.

In any case this is an extremely bad idea, as you give away complete control to your server. Not to mention the latency increase..

 

If it's the latter, I recommend a look at cURL.

Link to comment
Share on other sites

As it's written now, yes.

This is because you're making a HTTP call to the web server, which interprets the PHP code and sends out the resulting HTML. Just like a web browser.

 

You're much better off copying said file to your local server, and including it properly.

That way you don't have to wait for the HTTP response, which would increase the include time from nano seconds to milliseconds. Several thousand times slower, in other words.

Link to comment
Share on other sites

Additional information: I own the server that holds the file to include, and the file is basically designed to be ran on other pages... is there some way of enabling the one file to be ran on other servers?

 

If you haven't guessed by the way, I'm clueless on this topic.

Link to comment
Share on other sites

Easiest way would be to save it as a .txt file, and include it as such:

include ("http://www.server.invalid/source.txt");

That's a quick and dirty way of doing it, and perfect if you're not going to run it on the local server.

 

If you want to make sure it's always updated, and can be run on the local server as well, then you need to do something like this:

// At the top of "source.php"
if (isset ($_GET['show_source']) && $_GET['show_source'] == 1) {
    die (file_get_contents (__FILE__));
}

And then include it like this:

include ("http://www.server.invalid/source.php?show_source=1");

 

However, you should note that if you do it like this then everyone will be able to open it up and read the source code as well. So only do this if you're absolutely certain that:

[*]Latency times have no meaning, at all.

[*]It is 100% safe to show the source code to the entire world.

In the vast majority of times, you'll find that both of these will indeed make it ill adviced.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.