-1

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?

inf3rno
  • 24,976
  • 11
  • 115
  • 197
Sunil
  • 9
  • 1
    By the time your second domain reads the PHP file from the first domain, it has already been evaluated, and will be empty. It won't contain the PHP code in it for your second domain to read. – sevenseacat May 02 '14 at 12:19
  • Thanks for your instant replay . But how can I get the values of first page to second. – Sunil May 02 '14 at 12:20
  • `json_encode` your data and decode it in the second. It's webservice principle. – PoulsQ May 02 '14 at 12:22
  • Open the URL you're trying to include in a browser. That's the same thing PHP sees when trying to include the file. It's also a terrible idea to `include` over the network. It slows things down dramatically and poses security risks, since you're interpreting arbitrary stuff coming from the net as PHP code. – deceze May 02 '14 at 12:26
  • PoulsQ: Thanks for the advice, but as a fresher could u please give me the code – Sunil May 02 '14 at 12:31
  • Is there any specific reason why you would like to do this? Could you explain what you are trying to achieve by this? – Wesley De Keirsmaeker May 02 '14 at 13:04

1 Answers1

0

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
WolleTD
  • 372
  • 1
  • 7