Linto P D
Linto P D

Reputation: 9175

How to include one php file from other domain

I have two doamins(www.domain1.com,www.domain2.com) in the same server.

I have a index1.php in fist server. Now i need to include the file index2.php that resides in domain2,

how is possible using php code(include,require....)

Upvotes: 2

Views: 10698

Answers (6)

Herliansyah
Herliansyah

Reputation: 29

  1. setting php.ini (allow_url_include = On ) and then restart apache ( server 1 )
  2. rename external file from somefile.php to somefile ( server 2 )
  3. include the file on the php file like like : include "http://otherdomain.com/somefile"; ( server 1 )

Upvotes: 2

user2625447
user2625447

Reputation: 9

try to create a 3rd file using php with all permissions granted to copy/paste, you can start by replacing the .php to .txt then read all contents of the 2nd file and convert it to string.

convert the first file from .php to .txt and convert it to string as well..

finally create a 3rd file with both 1st file and 2nd file in proper place and rename it back to .php

rename all the files modified to .php

your done!

Upvotes: -1

Joel
Joel

Reputation: 2824

It is not possible to include a php file on another server, because this would be a security vulnerability (other sites setting variables and then including your files.) You can, however, use file_get_contents to get the output of a php file on another server.

Upvotes: 5

mway
mway

Reputation: 4392

This is not typically possible and something that you should consider not trying to do. If you control both domains and need shared code between them, consider versioning the shared code (with subversion, for example) and then checking out copies between them. Otherwise, if you don't control the other domain, you're out of luck.

Using cURL will also not help, as it will return the file after it has been processed. PHP's include() will allow the code to be inserted and executed within application flow, however, using cURL (or fopen(), file_get_contents(), etc) will obviously only return any script output as it will have been previously processed by the other server prior to your function of choice returning.

Upvotes: 0

AlexV
AlexV

Reputation: 23108

If both PHP users have access to each other space (unlikely), you will include the file like usual with include or require.

If not you can exchange data with something like cURL and then execute it with eval (watch out for security holes!!!). If you only need to excange data (like content) try XML with SimpleXML instead.

Upvotes: 0

chigley
chigley

Reputation: 2592

If they're same server, you can just include the file with an absolute file path. Hard to advise exactly without knowing your exact file path, but the code remains:

include('/path/to/your/file.php');

Upvotes: 4

Related Questions