Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

including php file from another server with php

Tags:

file

include

php

I have two PHP files located on different servers, one at http://www.mysite.com/main.php, the other at http://www.sample.com/includeThis.php.

I want to include the second file from the first one.

The content of the second file looks like this:

<?php $foo = "this is data from file one"; 

And the first file:

<?php include "http://www.sample.com/includeThis.php"; echo $foo; 

Is there any way I can do this?

like image 942
Desolator Avatar asked May 02 '10 07:05

Desolator


People also ask

Can I include PHP file from another server?

Nope, this setting is disabled/not allowed by default in most web servers (php. ini) so you can not use the include to include the files from a remote addresss for security reasons.

How do I include one PHP file in another PHP file?

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script.

How do I call a PHP page from another PHP page?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

How do I include two PHP files?

The include() statement is used to include a php file in another file. This way you can write a piece of code in a php file and can use it to multiple files through include() statement.


2 Answers

Nope, this setting is disabled/not allowed by default in most web servers (php.ini) so you can not use the include to include the files from a remote addresss for security reasons.

If you still want to allow inclusion of remote files, the directive allow_url_include must be set to On in php.ini

But again it is a bad practice, in a security-oriented point of view ; and, so, it is generally disabled (I've never seen it enabled, actually)

If you want to read the contents of a remote file though, you can use the file_get_contents function instead BUT this will be returned as pure HTML markup code, there won't be any server-side code.

like image 184
Sarfraz Avatar answered Oct 04 '22 09:10

Sarfraz


After reading your comments - in which you state that you want to do this as a means of copy protection - my answer is an emphatical, forget it. This is not how copy protection works.

The only thing you can do using include() is fetch source code from elsewhere to be interpreted on the local interpreter. This is childishly easy to crack: A malicious customer would to just have to echo() the fetched code.

Executing the remote script remotely (on your server) won't help you, because the state of that script (variables, functions...) won't be present in the script you call it from.

The options you have are:

  • Compiling / encoding / obfuscating the script, possibly requiring a specific PHP module to execute it (lots of questions about this on SO)

  • Creating a real web service (e.g. using SOAP) that runs on your server, and performs the requested operations

For what it's worth, though, I personally do not purchase, nor recommend to clients to purchase, encoded scripts and scripts that need to "phone home" in order to work. I believe in protecting your products through a stringent license agreement (that will scare business customers into buying your product, because the risks of getting caught stealing are too expensive.)

like image 21
Pekka Avatar answered Oct 04 '22 10:10

Pekka