views:

199

answers:

5

hi I have 2 php files each file on different server. lets say the first file called includeThis.php and the second called main.php

the first file is located in (http://)www.sample.com/includeThis.php

and the second located in (http://)www.mysite.com/main.php

so now what I want is to include the first file into my second file. the contents of the first file is like:

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

the second file like:

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

is there any way I can do this?

thanks in advance

+4  A: 

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.

Sarfraz
Yes, you could use `file_get_contents` and than `file_put_contents` to store it on your server, and than use `include` on it. Though it's a dangerous and risky thing to do IMO. But possible. And you can even extract certain code from it also and write only the code that you need from the file.
SoLoGHoST
I have the permissions to play with php.iniI have already enabled the allow_url_include.but when I use the mentioned method. I get nothing in the variable. means the variable is null. I dont know how to pass it to the main.phpI'm using this method to prevent people from cracking my scripts.so there will be part of the script on my server. thats why I need to use this method
ermac2014
A: 

When you're trying to go across domains as you have suggested, you're not actually including a file that's sat there ready to do - the process is different. The machine needs to bring back the file over http which isn't what the include statement is all about.

Also, if you're on shared hosting, PHP is often configured to prevent you from going outside of your own domain.

If you aren't under this restriction, one solution might be to use PHP to copy back a copy of the file from the other server, and then include it once it's sat in your domain. Another apporach might be to write a little "deployment" script that copies it everywhere it needs to be whenever you make changes...

Hope this helps...

Martin

Martin Milan
I dont want to copy the file on the other side. because as I said before I'm using this method to prevent people from cracking my scripts. so there will be part of the script on my server. thats why I need to use this method.
ermac2014
How does having a script on the internet help protect your scripts? I'm not sure I understand your approach...
Martin Milan
I have 2 scripts the first one is the main script which I will be selling to my customers. and the second has the main functions of the first script. so in order to use the script with functions. user must be authenticated in order to get the included functions.means the main script is depending on the functions script. if any cracker tries to null the script it will not work. in other words, it will be not possible to crack it because the include file is saved on my server. so no one can access it unless if he was a legit customer. I hope you understand what im trying to do here.
ermac2014
@ermac understood, but this won't work. @Martin is right.
Pekka
A: 

rename the first one to .txt
then think twice, are you sure you need cross-domain include

Col. Shrapnel
yea I need that
ermac2014
A: 

Use file_get_contents, to open up the file, append it to the second file like so:

$secondFile = file_get_contents('http://www.sample.com/includeThis.php');
file_put_contents('your_file', $secondFile, FILE_APPEND);

This will work if you want to put it at the end of your file. Than just do an include on your file.

Anyways, like I said, this is risky and dangerous IMO, especially if you aren't sure about the content it has inside of it.

Also, your_file will need to be an actual server path, not a URL.

SoLoGHoST
sorry man thats not what I need..
ermac2014
You can also use `file_put_contents` to store it on your server instead of writing it to a file.
SoLoGHoST
+1  A: 

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.)

Pekka
All in vain, I'm afraid
Col. Shrapnel