PHP:在两个站点之间传输大数组

发布于 2024-08-13 13:11:40 字数 275 浏览 2 评论 0原文

我正在尝试在 PHP 中的两个站点之间传输一个大数组。我是两者的管理员。 该数组是在一个站点上创建的,创建后我希望自动将用户重定向到另一站点,并传递处理后的数组。

我无法使用 SESSION 超级全局,因为它仅限于特定域。 GET 不适合,因为数组太长。 我不确定 POST 是否合适,以及是否有一种方法可以自动发送数据,而无需强制用户单击按钮并提交某些表单。我知道 javascript 可以用于此目的,但更喜欢使用更强大的东西。

我对 PHP 比较陌生,很想知道执行此操作的任何其他方法。 谢谢!

I'm trying to transfer a large array between two sites in PHP. I'm the admin in both.
The array is created on one site, and after its creation I wish to automatically redirect the user to the other site, and pass the processed array along.

I cannot use the SESSION superglobal, as it is limited to a specific domain.
GET is not suitable, as the array is too long.
I'm not sure if POST is suitable, and if there is a way to automatically send the data without forcing to user to click a button and submit some form. I know javascript can be used for this, but prefer to have something more robust.

I'm relatively new to PHP, and would love to hear of any other ways of performing this.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

他不在意 2024-08-20 13:11:40

最简单的方法是使用 HTTP 库(如 cURL)和设置并向其他站点发送 POST 请求。还包括用户的 IP 地址,这样您就可以关联发布的数据。如果没有 JavaScript,您就无法使用 POST 数据重定向用户。

对于上述方法,您可能需要注意的一件事是,根据其实现方式,用户可能会在数据到达之前到达。

HTTP 规范中定义的 POST 没有限制,但根据 php 配置,您可能会在其他服务器上处理它时遇到问题(取决于您所说的大)。 (我认为 POST 限制默认设置为 8MB)

The easiest way would be to use a HTTP library like cURL and setup and send a POST request to the other site. Also including the users IP address would allow you to associate the posted data. Without JavaScript you cannot redirect a user with POST data.

One thing you may want to be aware of with the above method is that depending on how it is implemented the user may arrive before the data does.

There is no limit on POST as defined in the HTTP specs, but you may run into issues handling it on your other server (depending on what you mean by large) depending on php configuration. (POST limit is I believe set to 8MB by default)

少钕鈤記 2024-08-20 13:11:40

通过 cURL 函数 发送 HTTP POST 请求并添加 serialize()数组到请求正文。

Send an HTTP POST request via cURL functions and add the serialize()ed array to the request body.

遗心遗梦遗幸福 2024-08-20 13:11:40

我会做这样的事情:

  • 在服务器 A 上生成一个令牌(例如 sha1(timestamp + session id + random()))
  • 使用 cURL 将序列化数组发布到服务器 B,并传递您生成的令牌
  • 在服务器 B 上,将序列化数据和令牌存储在数据库表中 - 字段:令牌 (CHAR)、数据 (BLOB)
  • 将用户重定向到 http://ServerB/? data_token=[步骤 1 中生成的令牌]
  • 服务器 B 从数据库中获取与令牌关联的数据,删除数据库条目,并将数组存储在新用户会话中。

I'd do something like this:

  • generate a token on Server A (e.g. sha1(timestamp + session id + random()))
  • use cURL to post the serialized array to the Server B, passing along the token you generated
  • On Server B, store the serialized data and token in a database table - fields: token (CHAR), data (BLOB)
  • redirect the user to http://ServerB/?data_token=[TOKEN GENERATED IN STEP 1]
  • Server B fetches the data associated with the token from the db, deletes the db entry, and stores the array in the new user session.
Saygoodbye 2024-08-20 13:11:40

好吧,如果它们都在同一台服务器上,那么您可以让其中一个劫持其他会话。我之前曾使用它跳转到安全服务器,在第一台主机上使用 session_id() 函数来获取会话,然后使用相同的函数在第二台主机上设置它。

请参阅 http://www.php.net/manual/en/function .session-id.php

Well, if they are both on the same server, you can have one hijack the others session. I have used this to jump to a secure server before, use the session_id() function on the first host to get the session, then use the same function to set it on the second host.

See http://www.php.net/manual/en/function.session-id.php

原谅过去的我 2024-08-20 13:11:40

我建议在创建“数组”后将其与一个 ID 关联(​​并存储在某处),然后使用此 ID 重定向到另一个。从站点 2 使用 ID,您可以调用站点 1 上的页面,该页面返回“Array”

I would suggest that after you create the "Array" you associate it with an ID(and store somewhere) and then redirect to the other with this ID. From site 2 using the ID, you can call a page on site 1 which returns the "Array"

梦旅人picnic 2024-08-20 13:11:40

您的问题:

Server(a)发送array(ar)Server(b)

我的解决方案:

  1. Server( a)Server(b)生成一个唯一的url(url),其中包含以例如json编码的Array(ar)使用 json_encode(ar)。这个Array(ar)应该使用例如mysql或只是一个简单的文本文件存储在url处。

    $uid = md5(uniqid(mt_rand(), true)); // 生成唯一的id
    
  2. 服务器(a)将浏览器重定向到也包含$uid的服务器(b)

    $url = "http://server-b/page"; // 页面的url
    header('位置:$url?uid=$uid');
    
  3. Server(b) > 从Server(a)上的url获取内容并将内容解码回Array(ar)

    $uid = $_GET['uid']; // uid
    $url_server_a = "http://server-a/webservice?uid=$uid";
    $ar = json_decode(file_get_contents($url_server_a));
    

Your problem:

sent array(ar) From Server(a) to Server(b)

My solution:

  1. Server(a) generates an unique url(url) for Server(b) which contains Array(ar) encoded in for example json using json_encode(ar). This Array(ar) should be stored at url using for example mysql or just a simple text file.

    $uid = md5(uniqid(mt_rand(), true)); // to generate unique id
    
  2. Server(a) redirects browser to Server(b) also containing $uid

    $url = "http://server-b/page"; // url to page
    header('Location: $url?uid=$uid');
    
  3. Server(b) gets the content from url on Server(a) and decodes content back to Array(ar)

    $uid = $_GET['uid']; // uid
    $url_server_a = "http://server-a/webservice?uid=$uid";
    $ar = json_decode(file_get_contents($url_server_a));
    
幸福还没到 2024-08-20 13:11:40

我想您可以将其序列化,将其保存为可从其他服务器访问的文件,然后从其他服务器再次加载它。这样,不需要用户执行任何操作,但您必须保护保存文件的目录以避免隐私问题。

编辑:我假设它们位于不同的服务器上,否则会更容易......

I guess you could serialize it, save it as a file that is accessible from the other server and load it again from the other server. That way, no user action would be required but you'd have to protect the directory where you save the file to avoid privacy problems.

Edit: I´m assuming they're on different servers, otherwise it would be even easier...

只为守护你 2024-08-20 13:11:40

如果是我,我会将信息存储在其他介质中:例如 memcache 类型的环境,或者两者都可以访问的数据库。

If it were me, I'd store the information in some other medium: a memcache type environment for example, or a database that both can access.

衣神在巴黎 2024-08-20 13:11:40

创建数组后,您可以快速生成一个页面,其中包含一个包含隐藏字段中的数据的表单。然后,该页面可以自动将表单(使用 method="POST")提交到您的重定向。

After the array is created you could quickly generate a page that has a form which contains the data in a hidden field. This page could then automatically submit the form (with method="POST") to your redirect.

南七夏 2024-08-20 13:11:40

您可以将数组解码为 JSON 并将链接发送到第二个服务器,其中包含临时 JSON 文件的下载,只需将 JSON 文件重新解码回 PHP,并且不必使用长 URL。

You could decode the array into JSON and send a link to the second server, which contains the download for the temporary JSON file, just re-decode the JSON file back into PHP and you do not have to use LONG URLs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文