I need to send data from one page to another, and I need it in a specific order. I'm pushing it to an array, so the original data might look like:
$arr[0] = '123_0.jpg';
$arr[2] = '123_2.jpg';
$arr[3] = '123_4.jpg';
$arr[7] = '123_8.jpg';
I'm finding two ways to retrieve this in the second page, but neither seem great so I'd like your feedback.
Option 1 I create the $arr array in a loop that reads from MySQL, so I could build a string inside of that loop to create:
$str = 'arr[0]=123_0.jpg&arr[2]=123_2.jpg&arr[3]=123_4.jpg&arr[7]=123_8.jpg';
Then I can include that in the query string for the next page.
Retrieving the data is a little tougher, though. I found that I can do this:
parse_str($_SERVER['QUERY_STRING'], $temp);
$arr = $temp['arr'];
print_r($arr);
/* Results:
*(
* [0] => 123_0.jpg
* [2] => 123_2.jpg
* [3] => 123_4.jpg
* [7] => 123_8.jpg
*)
*/
Downside here is that parse_str() plugs in all of the query string params to $temp, not just the ones I need. Since I can test to make sure that it fits the /\d+_\d+\.[a-z][a-z][a-z]/ format I don't think there's a concern of an attack, but I guess it's still possible.
Option 2 I can convert the original $arr to JSON to be included in the query string after the loop:
$str= 'arr=' . json_encode($arr, JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
Then the second page would simply use
$arr = json_decode($_GET['arr'], true); to convert the JSON back to the $arr array.
Downside there is that I'm not sure how to properly URI-encode that JSON string. I think that all of the flags I have listed here will do it right, but I'm not positive.
Is there a better option that I'm not considering?
If not, which of the two do you think is better?