Passing multiple values for same GET variable in URL

假如想象 提交于 2019-12-08 01:49:03

问题


I'm wondering if there is a cleaner way to pass multiple variables in a GET request than the following:

http://www.mysite.com/somepage?tags[]=one&tags[]=two&tags[]=three

I had thought about the following:

http://www.mysite.com/somepage?tags=one,two,three

Then using explode() to separate them.

Wondered if anyone had seen or used a better solution though?


回答1:


Using explode() is only reliable if the values of each tag will never contain whatever string it is you're exploding by (in this case ",").

I'd say it's safer to use tags[]=X&tags[]=Y.




回答2:


you can urlencode(json_encode(yourData)), then on the server json_decode

this solution will work with any complex data you may need to pass, not just the simple one you have here.




回答3:


i think the best solution is using json_encode(). But if you want to look nice (as a single string). You can encyrpt code to look like page?tags=HuH&ITBHjYF86588gmjkbkb. Simplest of doing it is

$tags = base64_encode(json_encode($tags_array));



回答4:


I suggest using mod_rewrite to rewrite like www.website.com/tags/tag1-tag2-tag3 or if you want to use only php www.website.com/tags.php?tags=tag1-tag2-tag3. Using the '-' makes it searchengine friendly (as for tags is often useful) and is also not as commonly used as a comma (thinking of the tagging itself).



来源:https://stackoverflow.com/questions/9941553/passing-multiple-values-for-same-get-variable-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!