views:

332

answers:

10

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!

+1  A: 

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

TrippyD
A: 

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...

jeroen
+3  A: 

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)

Yacoby
IE's limit is for GETs, and anyway that wouldn't affect the server :-) http://support.microsoft.com/kb/208427
NickFitz
Associating via IP address is a bad idea. Many users might share the same address.
Joel L
A: 

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"

ps
+2  A: 

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

Dor
A: 

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.

Narcissus
A: 

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.

Janek
+2  A: 

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.
Joel L
+1  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));
    
Alfred
A: 

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.

daemonfire300