PHP – Passing Array in URL and Retrieve with Get

Posted by on Sep 2, 2012 in PHP | 0 comments

To pass an array in a url use the following:

<?php $serialized = rawurlencode(serialize($args)); ?>
<a href="testpage.php?args=">Test Page</a>

To retrieve the array use the following:

<?php $args = unserialize(stripslashes(rawurldecode($_GET['args']))); ?>

Here is an explination of whats going on:

//create the associated array
$args['bid'] = 2;
$args['pid'] = 1806;
$args['mid'] = true;

//serialize and encode array to make it safe for transferring in the url
$serialized = rawurlencode(serialize($args));

<a href=”testpage.php?args=“>Test Page</a>

//url with included serialized and encoded array


//result of decoded array, notice the extra slashes in the serialized array
string(57) "a:3:{s:3:\"bid\";i:2;s:3:\"pid\";i:1806;s:3:\"mid\";b:1;}"


//strip slashes and make it ready to be unserialized
string(51) "a:3:{s:3:"bid";i:2;s:3:"pid";i:1806;s:3:"mid";b:1;}"


//unserialize the array
//now we have our origional associated array
array(3) { ["bid"]=> int(2) ["pid"]=> int(1806) ["mid"]=> bool(true) }
468 ad

Leave a Reply