I am looking to have a list of arguments passed across in an a URL.
$url['key1']=1;
$url['key2']=2;
$url['key3']=3;
$url['key4']=4;
$url['key5']=5;
$url['key6']=6;
$url['key7']=7;
Please Note I am trying to pass this in the URL in 1 GET variable. I know this would be better done by ?key1=1&key2=2&key3=3...etc but for reasons that are too complicated to try and explain they can't be in this format.
Any suggestions how I can convert this array into something that can be passed as 1 get var in a URL string?
Thanks in advance.
You could
serialize
them as key-value pairs when constructing the URL, putting the resultant serialized value in a single$_GET
variable (e.g.data=sfsdfasdf98sdfasdf
), thenunserialize
the$_GET["data"]
variable. You'll need to useurlencode
to make sure the resultant serialized values are URL-safe. Make sure you watch out for maximum URL lengths - 2083 characters in IE.However, unless you really can't use key-value pairs in URLs (per your question),
key1=foo&key2=bar...
is definitely the way to go.Could you solve your problem by saving the data as a HTML cookie? That way you don't have to modify the URL at all.
If you know the values in advance, you can set them from the server side when you send the user the page with your target link on it.
If you won't know the values until the user fills out a form it can still be done using JavascriptL When the user clicks the form submit you can set multiple cookies by making multiple javascript calls like: document.cookie = 'key1=test; expires=Mon, 7 Sept 2009 23:47:11 UTC; path=/'
The security model might give you some trouble if you are trying to pass this data from one domain to another though.
Try
http_build_query
:What it does is converting an array into a query string using the keys and automatically takes care of url-encoding.
EDIT:
Just read your additional requirement that it should be just one variable. So nevermind this answer. If your problem was the proper encoding though you might want to give this another try.
Hope that helps.
If you don't mind dropping the key names, you can use
EDIT Keeping the key names:
Then in your PHP script:
The recommendation to use
serialize()
is fine. If space is an issue, then use a combination ofbzcompress()
andserialize()
.However, there's a security considering that hasn't been brought up, and that's that the end user (who can see and edit this url) could manipulate the data within it. You may think it's difficult, but most of the PHP-attacking worms in the wild do this to some degree or another.
If letting the user directly manipulate any of the keys or values (or replacing it with an integer, or an object, or anything else), then you should protect your script (and your users) from this attack.
A simple solution is to simply use a shared secret. It can be anything; just so long as it's unique and truly secret (perhaps you should randomly generate it at install-time). Let's say you have in your config file something like this:
Then, you can digitally sign the serialized data created with:
$s=serialize($m)
using$k=sha1($s.SECRET)
and make the url value$k.$s
Then, before you
unserialize()
do this:This way, you know that
$m
is the same as the original value that you serialized.If you like, you can use the following drop-in replacements:
You can use
json_encode()
orserialize()
or
Using
json_encode
will usually give you a shorter string, but very old PHP version might not have thejson_decode
function available to decode it again.The final way would be to create your own custom encoding... it could be as simple a pipe-separated values: key1|1|key2|2|key3|3
This would give you the best option for a short URL, but is the most work.