开发者

Consolidating multiple GET parameters into a single value

开发者 https://www.devze.com 2023-03-10 17:25 出处:网络
Im not really sure what im looking for but currently in my system i send a long url like this: $name=1&option=2&field=4....

Im not really sure what im looking for but currently in my system i send a long url like this:

$name=1&option=2&field=4....

And its quite long. So if i have a list of values like:

  • name
  • option
  • field

can i put them into a string in which i can break at certain points eg by a slash or whatever.

And then encode the string so its completely random like, so i only have one field to send:

&data=JKHFGDKGLKJHFKDJHFKJDHFKHDF

Then finally i can decode the other side and break apart.

Is there a pre-built function to do this?

WHAT IT IS:

im sending data to paypal, but i have a few custom variables i wish to send, now for some reason my开发者_开发技巧 IPN isnt geting them, not sure why, but if i add one called custom it get to the IPN fine. So i thought if i just send one called custom in a random format and then decode?


You could simply Base64-encode your data.

$a = array('name' => '1', 'option' => '2', 'field' => '4');
$temp = json_encode($a);       // convert array to string
$data = base64_encode($temp);  // encode string

output:

"eyJuYW1lIjoiMSIsIm9wdGlvbiI6IjIiLCJmaWVsZCI6IjQifQ=="

To send this in an URL, you must encode it once more (like you must encode all data you would send in a URL)

$url = $url . "&data=" . urlencode($data)

The intermediary step through JSON ensures your data will kep its structure and will be easily decodeable on the receiving side.

On the downside: Your URL will be longer.


Use base64_encode and then base64_decode, this will solve your problem.


If you don't know what you are looking for we can't imagine that lol.

Anyway If i got you, you can do this, considering you have built your string:

$string ="$name=1&option=2&field=4";

You could pass it as a single param with:

$data = url_encode($string);   
<a href="url?data={$data}"></a>

Other than url_encode you can use base64_encode


As the commenter (Bobby) says - consider using POST when you want to send larger sets of data and prevent your URL from becoming unmanageably long or ugly.

GET variables are handy for providing the user with a page they can bookmark directly which is desirable in some cases, such as on a search page with a query string and/or filters already filled in, so that the user can return to a search and check for new results periodically without having to reset all of their choices.

POST variables are better if you don't need that sort of functionality, you don't need to encode/decode them for URLs and they can't be bookmarked directly (which is also desirable in many cases).

To answer your original question though, if you really, really had to send the variable(s) on the URL and you wanted to just send one apparently random string, I suggest writing a couple of encode/decode functions of your own (since I assume the object of the excercise is not to encrypt it against tampering, just to make your URLs friendlier). This will be all the easier if there are restrictions to what the variables can be, and more difficult if they can be absolutely anything.

For example - if you have the following vars and possible settings:

  • var1 (apple, banana, orange)
  • var2 (car, motorbike, bicycle)
  • var3 (red, yellow, green)

Normally, you'd make a URL like:

http://www.mysite.com/page.php?var1=banana&var2=car&var3=green

If you assign the variables to numbers (for example) so that var1, var2 and var3 would be 1, 2 or 3 - then you could send over a URL like:

http://www.mysite.com/page.php?vars=213

Break it down at the other end into single numbers and convert those back into 'banana', 'car', and 'green'.

But seriously.... I'd look at POST first unless there is very specific reason why you would use this sort of approach - i've used it before for shortening a URL to make it more sharable on social media and forums.

0

精彩评论

暂无评论...
验证码 换一张
取 消