Reputation: 647
I want to use the PHP "include" function to include a file from a different domain than my main script, and use variables from the main script in the included file.
Something like this:
index.php:
<?php
$hello = "Hello";
$world = "World";
include "http://example.com/myfile.php";
?>
myfile.php (from a different domain)
<?php
echo $hello . " " . $world;
// Should output "Hello World"
?>
Will this work, or does it depend on the server settings/permissions?
Upvotes: 2
Views: 4138
Reputation: 647
As several answers point out, allow_url_include
has to be enabled, and that's not an option in my case. I ended up doing this:
index.php:
<?php
$hello = "Hello";
$world = "World";
echo file_get_contents("http://example.com/myfile.php?hello=$hello&world=$world");
?>
myfile.php (from a different domain)
<?php
$hello = $_GET['hello'];
$world = $_GET['world'];
echo $hello . " " . $world;
// Should output "Hello World"
?>
It works fine for what I need it to do, but I'm sure the solution can be improved.
Upvotes: 1
Reputation: 47169
It will work depending on whether or not allow_url_include
is enabled in php.ini
. Otherwise you can try the hacky way of doing it by replacing the current include
line with:
echo file_get_contents('http://example.com/myfile.php');
Upvotes: 1
Reputation: 219804
From the manual:
Using remote files
As long as allow_url_fopen is enabled in php.ini, you can use HTTP and FTP URLs with most of the functions that take a filename as a parameter. In addition, URLs can be used with the include, include_once, require and require_once statements (since PHP 5.2.0, allow_url_include must be enabled for these). See Supported Protocols and Wrappers for more information about the protocols supported by PHP.
Upvotes: 1