views:

43

answers:

3

I have a series of web sites all hosted on the same server with different domains. I want to host some common PHP scrips and then be able to call these from the other domains.

Im am a bit fresh with my php so pls excuse code attempts - I have tried iterations of the following which may try and help you understand what I am aiming for!

from within php tags ...

include('http://www.mydomain/common_include.php?show_section=$section');

$show_section = $_GET['show_section'];
include('http://www.mydomain/common_include.php');//Then $show_section would be available to the included file/cod

Finally I have tried pulling in the include which contains a function then trying to run that include from the parent script.

  • I would much prefer to keep this PHP orientated rather than getting involved with the server (file systems etc (but I can change permissions etc)
  • I can but would prefer not to just upload the same library to each of the domains separately
  • I understand PHP is run on the server hence maybe problematic to include scripts across onto another server.

Thanks in advance.

#

EDIT OK OK - I get that its bad practice so will not do it....THANKS VERY MUCH FOR THE QUICK ANSWERS. However is there any other recommendations of how to esentially show this basic php app on all of the sites with out haveing to add the files to the root of each site? Just to prevent massive script duplication...(thinking out loud call the scripts in from a db or anyother soloutions) Again thanks for your assistance

+2  A: 

That would be a huge security risk if you could just include remote PHP files to your own projects. The PHP gets parsed before the server sends it to you so cross-domain includes would only contain the output the script generates. The only way to include PHP files so that they can be executed is via local filesystem.

If you look at PHP.net's documentation about include, you can find this:

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 List of Supported Protocols/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.

Which pretty much explains the whole thing.

Tatu Ulmanen
A: 

That is very bad practice.
Actually you're including not PHP but just HTML code.

Include files, not urls. It is possible for the same server.
Just use absolute path to these files.

Col. Shrapnel
Sorry can you pls clarify 'Include files, not urls' do you mean using php 'file_get_content's or 'fopen' ? Thanks
@user256888 I mean use the same include. but use a **filename** as argument, not URL.
Col. Shrapnel
Include files means include('/usr/local/apache/htdocs/common_include.php') not include('http://www.mydomain/common_include.php')
Mark Baker
A: 

Apart from the fact that it's a bad practice you should first check if include allows URLs if you really want to do that.

If however all the sites that need to use the script, you could put the script somewhere in a directory accessible by the user that executes php and add that dir to the php.ini include_path property (can also be done at runtime)

(Or you could create a php extension and load it as extension)

Redlab