PHP:在两个站点之间传输大数组
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
最简单的方法是使用 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)
通过 cURL 函数 发送 HTTP POST 请求并添加 serialize()数组到请求正文。
Send an HTTP POST request via cURL functions and add the serialize()ed array to the request body.
我会做这样的事情:
http://ServerB/? data_token=[步骤 1 中生成的令牌]
I'd do something like this:
http://ServerB/?data_token=[TOKEN GENERATED IN STEP 1]
好吧,如果它们都在同一台服务器上,那么您可以让其中一个劫持其他会话。我之前曾使用它跳转到安全服务器,在第一台主机上使用 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
我建议在创建“数组”后将其与一个 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"
您的问题:
从Server(a)发送array(ar)到Server(b)
我的解决方案:
Server( a) 为Server(b)生成一个唯一的url(url),其中包含以例如json编码的Array(ar)使用 json_encode(ar)。这个Array(ar)应该使用例如mysql或只是一个简单的文本文件存储在url处。
服务器(a)将浏览器重定向到也包含$uid的服务器(b)
Server(b) > 从Server(a)上的url获取内容并将内容解码回Array(ar)
Your problem:
sent array(ar) From Server(a) to Server(b)
My solution:
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.
Server(a) redirects browser to Server(b) also containing $uid
Server(b) gets the content from url on Server(a) and decodes content back to Array(ar)
我想您可以将其序列化,将其保存为可从其他服务器访问的文件,然后从其他服务器再次加载它。这样,不需要用户执行任何操作,但您必须保护保存文件的目录以避免隐私问题。
编辑:我假设它们位于不同的服务器上,否则会更容易......
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...
如果是我,我会将信息存储在其他介质中:例如 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.
创建数组后,您可以快速生成一个页面,其中包含一个包含隐藏字段中的数据的表单。然后,该页面可以自动将表单(使用 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.
您可以将数组解码为 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.