PHP使用全局变量

PHP使用全局变量,php,xml,variables,global,xml-parsing,Php,Xml,Variables,Global,Xml Parsing,我对PHP全局变量的使用有一些问题。我从StackOverflow搜索过,但没有一个像我的一样(至少我没有找到) 我有两个php页面Index.php和Account.php。Index.php通过ajax调用account.php。php必须从其他URL接收一些xml数据并将其存储。之后Index.php必须使用它 在index.php上: $("#login-submit").click(function(){ $("#span-login-loading").css('

我对PHP全局变量的使用有一些问题。我从StackOverflow搜索过,但没有一个像我的一样(至少我没有找到)

我有两个php页面Index.php和Account.php。Index.php通过ajax调用account.php。php必须从其他URL接收一些xml数据并将其存储。之后Index.php必须使用它

在index.php上:

$("#login-submit").click(function(){
          $("#span-login-loading").css('visibility','visible');
          var dataString = 'user='+ $("#login-name").val() + '&pass=' + $("#login-pass").val();
          $.ajax({
            type: "POST",
            url: "pages/account.php",
            data: dataString,
            success: function(data) {
              $("#login-form").html(data);
              $("#span-login-loading").css('visibility','hidden');
              if (data.substr(0, 12) == "Logged in as"){
                $("#div-login-submit").html('<a href="pages/account.php?logout=1" id="logout">Гарах</a>');
              }
            }
          });
          return false;
        });
$xml_data = "<loginRequest>
        <username>" . $_POST['user'] . "</username>
        <password>" . $_POST['pass'] . "</password>
      </loginRequest>";
    $url = "url here";

    $username = "name here";
    $password = "pass here";
    $process = curl_init($url);
    curl_setopt($process, CURLOPT_HEADER, 0);
    curl_setopt($process, CURLOPT_TIMEOUT, 10);
    curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
    curl_setopt($process, CURLOPT_POSTFIELDS, $xml_data);
    $return = curl_exec($process);
    $info = curl_getinfo($process);

    $xml = simplexml_load_string($return);
    curl_close($process);
如果我从中删除(字符串),它会产生类似于致命错误的错误:在第0行未知的堆栈帧中抛出异常,但我需要的是pass对象,而不是字符串

$xml = simplexml_load_string($return);
echo $xml; // add this line at the end of account.php
curl_close($process);
此外,当您希望从其他php页面中获取$xml的内容时,可以通过获取accounts.php的processed页面来填充变量,如下所示

$xml = file_get_contents('http://yourserver.com/pages/account.php');
以及将内容转换为xml对象:

$xml = simplexml_load_string(file_get_contents('http://yourserver.com/pages/account.php'));

一个快速的解决方案应该经过序列化:您可以将xml对象保存到会话中,如下所示:

$_SESSION['whatever']=$xml->asXML();
并将其检索为

$xml = simplexml_load_string($_SESSION['wathever']);
注意: 序列化是对该术语的不当使用,因为simplexml没有实现可序列化接口(从文档中可以看出),因此我们必须序列化它
依靠simplexml提供的功能

您能重写第二段吗?有3页还是2页?user.php、index.php、account.php。还有哪个页面是php.php?我将在index.ph中的其他部分以及其他页面中使用这个$xml,而不仅仅是在ajax返回的地方。现在,我正在处理您的示例。但是我现在如何使用$xml呢?我尝试了
echo$xml->result
,但它说
注意:尝试获取非对象的属性
。另外
文件获取内容
获取页面内的所有代码。未打印内容。使用服务器的完整路径,而不是文件的本地路径,获取文件内容将获取打印内容
$xml = simplexml_load_string($_SESSION['wathever']);