Multiple Variables into 1 in a URL

2019-06-14 04:54发布

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.

6条回答
SAY GOODBYE
2楼-- · 2019-06-14 05:24

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), then unserialize the $_GET["data"] variable. You'll need to use urlencode 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.

查看更多
淡お忘
3楼-- · 2019-06-14 05:34

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.

查看更多
【Aperson】
4楼-- · 2019-06-14 05:35

Try http_build_query:

$url['key1']=1;
$url['key2']=2;
$url['key3']=3;
$url['key4']=4;
$url['key5']=5;
$url['key6']=6;
$url['key7']=7;

echo http_build_query($url);
//echos key1=1&key2=2&key3=3&key...

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.

查看更多
相关推荐>>
5楼-- · 2019-06-14 05:36

If you don't mind dropping the key names, you can use

http://example.com?url[]=1&url[]=2&url[]=3

EDIT Keeping the key names:

http://example.com?values[]=1&values[]=2&values[]=3&keys[]=1&keys[]=2&keys[]=3

Then in your PHP script:

$url = array_combine($_GET['keys'], $_GET['values']);
查看更多
戒情不戒烟
6楼-- · 2019-06-14 05:43

The recommendation to use serialize() is fine. If space is an issue, then use a combination of bzcompress() and serialize().

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:

define('SECRET', 'unoqetbioqtnioqrntbioqt');

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:

$v=substr($input,0,40);
$s=substr($input,40);
if ($v != sha1($s.SECRET)) { die("invalid input"); }
$m=unserialize($s);

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:

define('SECRET','buh9tnb1094tib014'); // make sure you pick something else
function secureserialize($o) {
  $s=serialize($o);
  return sha1($s.SECRET).$s;
}
function secureunserialize($i) {
  $v=substr($i,0,40);$s=substr($i,40);
  if ($v!=sha1($s.SECRET)){die("invalid input");}
  return unserialize($s);
}
查看更多
Animai°情兽
7楼-- · 2019-06-14 05:44

You can use json_encode() or serialize()

$myUrl = 'http://www.example.com/?myKey=' . urlencode(json_encode($url));

or

$myUrl = 'http://www.example.com/?myKey=' . urlencode(serialize($url));

Using json_encode will usually give you a shorter string, but very old PHP version might not have the json_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.

查看更多
登录 后发表回答