Remote includes

One file, multiple domains?

Sooo.. you want to use one single file on a multitude of domains? Although it is not the best way to organize things, it is certainly possible to do this! Upon request of a reader, here a little example on how to use a shared file on multiple domains. Let start off with creating an example file. This we save in a test folder, and call it test.inc. We do NOT call it test.php. as we do not want to pass the script through the PhP processor. Please not: Do NOT include any references to passwords or database acces codes!

1
2
3
$line = "This is a footer. What we here do is an ";
$var = "example";
echo $line.$var;
$line = "This is a footer. What we here do is an ";
$var = "example";
echo $line.$var;

Effectively, this just sets 2 variables, and combines them to a single line which is echoed out to screen. Remember: what is in the file is not relevant. It is just an example on how to work this situation.
Subsequently we will continue to create the file which is to call the test.inc. Let’s call this remote.php.

1
2
3
4
<?php
$footer = file_get_contents("http://localhost/test/test.inc");
echo eval($footer);
?>
<?php
$footer = file_get_contents("http://localhost/test/test.inc");
echo eval($footer);
?>

This little bit of code does the magic. First we open an URL to read from. This URL is the URL of the file we made earlier. Note that even though both files sit on the same server, this is not relevant from a processing perspective: As soon as we make the call through an URL, the file could just as well be sitting on a server on the other side of the world. The file_get_contents command will open the file, and place the content in a variable. Subsequently the script uses eval(). Eval takes the content of any variable, and processes it as if it were PhP code.

Security

In order to make sure this file is not used by any site other than your own sites, we may need to add a littlee bit of security. Place the include file in a separate folder on your server. In this folder we place a htaccess file, in which we place the following command:

1
2
3
4
5
6
7
<limit GET POST PUT>
 
  Order allow,deny
  Deny from all
  allow from <strong>192.168.0.0</strong>
 
</timit>
<limit GET POST PUT>

  Order allow,deny
  Deny from all
  allow from <strong>192.168.0.0</strong>

</timit>

What this does, is block all users from accessing files in this folder. Only machines with the IP as mentioned in the allow from line will be allowed to access the file. aturally, as multiple sites need the file, you will need to add IP adresses to the .htacces file.

If you have any questions or would like to see a specific example / tutorial on this site, please contact me!

Leave a Reply