Including PHP File from Another Server With PHP

including php file from another server with php

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.

Call PHP file located in another server with parameter and read variable values in another server PHP

Getting the response of another server using include is disabled by default in the php.ini for security reasons, most likely you won’t be able to use it. Use file_get_contents instead.

In your file.php you can make a json response using your data and echo it:

<?php
$email = $_REQUEST['email'];
$name = $_REQUEST['name'];
$a = "testing";

header('Content-type: application/json');
echo json_encode(
'email' => $email,
'name' => $name,
'a' => $a
);
?>

And in the A.php you need to parse the json string to get your data:

<?php
$data = json_decode(file_get_contents('http://10.1.1.12/a/file.php?email=aaa@a.com&name=abc'));
echo $data['email'], ' ', $data['name'], ' ', $data['a'];
?>

PHP // Include file from another server if the standard-include server is down

Short answer:

You're more than likely going to want to refactor your code.

Longer answer:

If you truly want to do this at the server level then you're looking at implementing a "failover." You can read the wikipedia article, or this howto guide for a more in-depth explanation. To explain it simply, you would basically need 3 web servers:

  1. Your include server
  2. A backup server
  3. A monitoring / primary server

It sounds like you've already got all three, but bullet three would ideally be a service provided through a third-party for extra redundancy to handle the DNS (there could still be downtime as DNS updates are being propagated). Of course, this introduces several gotchas that might have you end up refactoring anyway. For example you might run into load balancing challenges; your application now needs to consider shared resources between servers such as anything written to disk, sessions or databases. Tools like HAProxy can help.

The simpler option, especially if the domains associated with the includes are hidden from the user, is to refactor and simply replace bullet three with a script similar to your get_data function:

function ping($domain) {
$ch = curl_init($domain);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
return $response ? true : false;
}

$server1 = 'http://example.com';
$server2 = 'http://google.com';

if (ping($server1)) {
return $server1;
} else {
return $server2;
}
exit;

This would require you to update all of your files, but the good news is that you can automate the process by traversing all of your PHP files and replace the code via regex or by using a tokenizer. How you implement this option is entirely dependent on your actual code along with any differences between each site.

The only caveat here is that it could potentially double the hits to your server, so it would probably be better to use it in such a way that you're setting an environment or global variable and then have it execute periodically through cron.

I hope that helps.

Creating a file on another server using PHP file functionality

There are many ways to do this. I'd pick the first one myself because it's easiest to set up:

  • If you have PHP+Apache on another server, just call some script on the other server using file_get_contents with http URL as filename or use cURL if you need to POST file contents as well.
  • If the servers are in same network (LAN, VPN) you can use Windows shares/Samba or NFS to mount a remote directory to you local filesystem and simply write to file directly using fopen/fwrite functions
  • Use SSH via SCP or SFTP


Related Topics



Leave a reply



Submit