druzuz321103 2014-05-02 12:17 采纳率: 100%
浏览 12

使用include从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?

  • 写回答

1条回答 默认 最新

  • duandiaoqian5795 2014-05-02 12:29
    关注

    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
    
    评论

报告相同问题?