简体   繁体   English

将变量从php传递到php

[英]Passing variable from php to php

I was wondering how to send a php variable from one server to another php script on another server? 我想知道如何将php变量从一台服务器发送到另一台服务器上的另一台php脚本?

I have 2 php scripts on 2 different server and one must send vars to the other. 我在2个不同的服务器上有2个php脚本,一个必须将var发送给另一个。 I've been searching with little luck. 我一直运气不好。 Would appreciate any help. 将不胜感激。

You could achieve that using curl and sending the variable as a GET value. 您可以使用curl并将变量作为GET值发送来实现。

Something like this: 像这样:

$data = "data you want to send";
$data = urlencode($data);
$url = "http://example.com?data=" . $data;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);

Let's assume $data = "foobar" 假设$data = "foobar"

Doing the above from a PHP script would be the same as someone visiting http://example.com?data=foobar from a browser. 从PHP脚本执行上述操作与从浏览器访问http://example.com?data=foobar的用户相同。

You could obviously send it to any script using the url: 您显然可以使用url将其发送到任何脚本:

http://example.com/yourscript.php?data=foobar

At yourscript.php you can get the data at $_GET['data'] , do some input validation to ensure it is being sent from your script and not from someone else via a browser (more on that later) and then proceed with your script. yourscript.php您可以在$_GET['data']处获取数据,进行一些输入验证,以确保它是从脚本而不是通过浏览器从其他人发送的(稍后再介绍),然后继续进行操作。脚本。

For this to work, yourscript.php will have to reside in the public html folder of youtr webhost so it is accessible to your other script. 为此, yourscript.php必须驻留在youtr虚拟主机的public html文件夹中,以便其他脚本可以访问它。

SECURITY 安全

Whether you are passing the data over GET or POST , someone else can send (possibly malicious) data to your script as well. 无论您是通过GET还是POST传递数据,其他人也可以向脚本发送(可能是恶意的)数据。 Thus, when yourscript.php receives data, there needs to be a way for it to ensure you are the sender of the script. 因此,当yourscript.php接收数据时,需要一种方法来确保您是脚本的发送者。 An easy way to achieve this is: decide on any arbitrary number known only to you, say, 12. 一种简单的实现方法是:确定只有您知道的任意数字,例如12。

Concatenate the number with the data you are passing and calculate the md5 hash and send it as another get variable. 将数字与您要传递的数据连接起来,然后计算md5哈希并将其作为另一个get变量发送。

In this case, you would calculate md5("12foobar") 在这种情况下,您将计算md5("12foobar")

and the URL would be: http://example.com/yourscript.php?data=foobar&auth=hash 网址为: http://example.com/yourscript.php?data=foobar&auth=hash : http://example.com/yourscript.php?data=foobar&auth=hash

When yourscript.php receives the data, it calculates the same hash (using the number 12, known to no one else) and if the hash it calculates matches with $_GET['auth'] , you can be sure you sent the data. yourscript.php接收到数据时,它计算出相同的哈希值(使用数字12,这是其他人所不知道的),并且如果它计算出的哈希值与$_GET['auth']相匹配,则可以确保发送了数据。

If someone tried to imitate you and send data, they would not know how you calculate the hash, and would thus send the wrong hash. 如果有人试图模仿您并发送数据,他们将不知道您如何计算哈希值,因此将发送错误的哈希值。

PS 聚苯乙烯

Another way to ensure rock solid security, would be to just check the IP address of the user-agent at $_SERVER['REMOTE_ADDR'] . 确保坚如磐石的安全性的另一种方法是,仅检查$_SERVER['REMOTE_ADDR']用户代理的IP地址。 If it is the IP address of the webhost where your other script resides, then you know it is you. 如果这是您其他脚本所在的Web主机的IP地址,那么您就知道是您自己。 I haven't thought this method through, so there might be some loopholes. 我还没有考虑过这种方法,因此可能存在一些漏洞。

您可以使用GET查询字符串( second_php?var=value )或使用带有POST方法的curl连接来执行此操作,然后通过POST发送数据。

You should probably use SOAP . 您可能应该使用SOAP It's used for remote function calls and it brings you little more overhead than just calling http requests, but it also brings you guarantee that remote function will be executed (or will cause error), it will directly return whatever datatype you need and I believe that's what this technology was developed for :) 它用于远程函数调用,它给您带来的不仅仅是调用http请求的开销,而且还可以确保远程函数将被执行(或将导致错误),它将直接返回所需的任何数据类型,我相信这项技术是为了什么而开发的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM