Can i include php file from another server?

I have two PHP files located on different servers, one at //www.mysite.com/main.php, the other at //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 "//www.sample.com/includeThis.php"; echo $foo;

Is there any way I can do this?

NikiC

99.4k36 gold badges188 silver badges224 bronze badges

asked May 2, 2010 at 7:41

2

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.

answered May 2, 2010 at 7:45

SarfrazSarfraz

370k73 gold badges529 silver badges574 bronze badges

3

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

answered Aug 8, 2010 at 13:21

PekkaPekka

434k137 gold badges965 silver badges1078 bronze badges

0

I wonder if the OP ever found a solution for himself. As far as I know, the only way to work this would be to have all your client accounts on the same server as the scripts you want to include - I've done something similar:

/path_to_myserver_root/httpdocs/clients/client01/wwwroot/scriptA.php /path_to_myserver_root/httpdocs/clients/client02/wwwroot/scriptA.php ETC....

THEN: /path_to_myserver_root/privatefiles/myapp/scriptB.php

wwwroot is where each client domain points.

scriptA.php has some business logic then includes scriptB.php for it's functions with the full path above:

require('/path_to_myserver_root/privatefiles/myapp/scriptB.php')

scriptB.php resides in a private protected dir on the server, inaccessible by http, and not traversable by the clients.

Now mind you, my reasons for doing this is to maintain version consistency across multiple accounts, not to withhold some proprietary magical php code from my clientele - But I suppose it could be implemented for that purpose.

Meh, YMMV.

answered May 24, 2012 at 3:43

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

answered May 2, 2010 at 7:49

Martin MilanMartin Milan

6,3002 gold badges31 silver badges44 bronze badges

3

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

answered May 2, 2010 at 7:50

Your Common SenseYour Common Sense

156k39 gold badges208 silver badges330 bronze badges

0

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

$secondFile = file_get_contents('//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.

answered May 2, 2010 at 7:57

SoLoGHoSTSoLoGHoST

2,5737 gold badges29 silver badges51 bronze badges

1

How can I transfer file from one server to another in PHP?

Kickstart HTML, CSS and PHP: Build a Responsive Website $file = "file_name. jpg"; $destination = fopen("ftp://username:password@example.com/" . $file, "wb"); $source = file_get_contents($file); fwrite($destination, $source, strlen($source)); fclose($destination); The image needs to be transferred to an FTP server.

How do I call a PHP file from another folder?

The easiest way to include a file from another folder is to use the absolute path (specify the full path to the file). For example, include "C:\\http\\lib\\script. php"; That covers the quick basics, but let us walk through more details in this guide – Read on!

Can you run PHP files on a server?

The preferred way of running PHP files is within a web server like Apache, Nginx, or IIS—this allows you to run PHP scripts from your browser. That's how all PHP websites work! The other way is to run PHP scripts on the command line, and it doesn't require you to set up a web server.

Can we run PHP file without local server?

You can make a PHP script to run it without any server or browser. You only need the PHP parser to use it this way. This type of usage is ideal for scripts regularly executed using cron (on *nix or Linux) or Task Scheduler (on Windows). These scripts can also be used for simple text processing tasks.

Chủ đề