在不同服务器上的两个文件之间切换php变量


switch php variables between two files on different servers

好的,所以我在服务器#1上有a.php,在服务器#2 上有b.php

假设php包含:

<?php
$google = 'www.google.com';
?>

和b.php包含:

<?php
require('http://[server #1]/a.php');
echo $google;
?>

基本上这是行不通的,如何在不编辑php.ini的情况下使其工作?:D

使用JSON读取全局变量并将其导出为JSON字符串,然后在接收端使用extract()将变量恢复到全局范围中。

服务器A 上的config.php

// Define your Variables
$google = 'www.google.com';
$string = 'foobar';
$int = 1;
$bool = true;
// JSON Export
echo json_encode(array_diff_key(get_defined_vars(),array_flip(array('_GET','_POST','_COOKIE','_FILES','_ENV','_REQUEST','_SERVER'))));

服务器B 上的application.php

// JSON Import
extract(json_decode(file_get_contents('http://www.domain.com/config.php'),true));
// Now test your Variables
var_dump($google); // Now the $google variable exists
var_dump($string);
var_dump($int);
var_dump($bool);

您可以在代码中发明自己的安全机制来满足您的需求。我更希望您在发送端和接收端都使用特定的变量列表,这种使用get_defined_vars()和extract()的方法并不理想,但由于您针对的是特定的URL,因此您可以控制风险。

您根本无法在其他服务器上访问远程php文件的源代码。最好的方法是创建一个API类型的东西(设置一个特殊的密钥):

$secretkey = "stackoverflow";
if(isset($_GET['secretkey']) && $_GET['secretkey'] == $secretkey){
$google = "www.google.com";
//Either if a specific string is requested, then reveal that:
if(isset($_GET['req'])){
$request = $_GET['req'];
//reveal the value of string Google
if($request == "google"){
echo $google;
}
}else{
//Or reveal the whole source code if there is no specific request
show_source(__file__);
}
}else{
die("Access denied.");
}