I am trying to get the value of a variable from one domain to another using php include , but it is not working.
firstdomain.com/test1.php
<?php
$i=10;
?>
seconddomain.com/test2.php
<?php
include_once "firstdomain.com/test1.php";
echo $i;
?>
I have enabled allow_url_include
, so I can now include the firstdomain.com/test1.php file to seconddomain.com/test2.php, but how do I get the value of $i
?
You can't include php code code cross-domain, because the remote server evaluates the file before sending it.
Anyway, you can make the first script output the variables you need, e.g. as a JSON-String and read the output into a JSON object again.
firstdomain.com/foo.php
<?php
$data["i"] = 1;
echo json_encode($data);
?>
Outputs (to your web browser as well as to your other PHP script):
{"i":1}
seconddomain.com/bar.php
<?php
$data = json_decode(file_get_contents("http://firstdomain.com/foo.php"));
echo $data["i"];
?>
Outputs:
1