enridp Posted March 22, 2011 Share Posted March 22, 2011 Hi ! I need to pass a lot of variables using GET, and many of them are related, so I was thinking in two options: 1) Pass them as array: example.php?categories[]=cat1&categories[]=cat2 2) Pass them in a single parameter, and split it in PHP (a custom and simpler serializer) example.php?categories=cat1|cat2 What do you think is better? (or maybe there is a 3rd option...) The second method seems more readable and short, the first easier to construct and maybe faster because we don't need to "unserialize". But I'm looking for better reasons. I'm thinking in a possible advantage of the first method over the second for SEO, because if Google is smart enough to understand that categories[] are all the same, then can understand that: example.php?categories[]=cat1&categories[]=cat2 is equal to: example.php?categories[]=cat2&categories[]=cat1 I think that is more difficult for Google to understand that with: example.php?categories=cat1|cat2 and example.php?categories=cat2|cat1 Regards!! Enrique. Quote Link to comment https://forums.phpfreaks.com/topic/231337-best-way-to-pass-a-lot-of-data-with-get/ Share on other sites More sharing options...
ToonMariner Posted March 22, 2011 Share Posted March 22, 2011 definitely the array structure - you can access it without having to do any further processing... Quote Link to comment https://forums.phpfreaks.com/topic/231337-best-way-to-pass-a-lot-of-data-with-get/#findComment-1190658 Share on other sites More sharing options...
enridp Posted March 22, 2011 Author Share Posted March 22, 2011 Yes, the problem is that I never see a URL like that, with arrays, but I see frequently the other URL style. And that make me think that maybe I'm doing something wrong. Do you know any big site with array in their URLs? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/231337-best-way-to-pass-a-lot-of-data-with-get/#findComment-1190783 Share on other sites More sharing options...
enridp Posted March 23, 2011 Author Share Posted March 23, 2011 Well, I was trying the URLs with brackets, and I was happy because php has two very useful functions for that: parse_str and http_build_query but with the last one we get a result like this: user%5Bname%5D=Bob+Smith&user%5Bage%5D=47&user%5Bsex%5D=M& user%5Bdob%5D=5%2F12%2F1956&pastimes%5B0%5D=golf&pastimes%5B1%5D=opera& pastimes%5B2%5D=poker&pastimes%5B3%5D=rap&children%5Bbobby%5D%5Bage%5D=12& children%5Bbobby%5D%5Bsex%5D=M&children%5Bsally%5D%5Bage%5D=8& children%5Bsally%5D%5Bsex%5D=F&flags_0=CEO It's a real pain, because the brackets ( "[ ]" ) are escaped. I was planing to "unescape" the brackets, but according to RFC 3986 and later on section "3.2.2. Host" we learn in the fourth paragraph that: "A host identified by an IP literal address … is distinguished by enclosing the IP literal within square brackets ("[" and "]"). This is the only place where square bracket characters are allowed in the URI syntax." So square brackets in our uri query will make it invalid. Maybe that's the reason because is not frequent to see URLs with arrays, because they are very long, agly and confusing. Quote Link to comment https://forums.phpfreaks.com/topic/231337-best-way-to-pass-a-lot-of-data-with-get/#findComment-1191110 Share on other sites More sharing options...
possien Posted March 23, 2011 Share Posted March 23, 2011 I used arrays in a couple of pages that ask "how many" and pushes that count into an array. http://www.vvaarizona.org/combined_disability.php?percent[]=10&percent[]=20&percent[]=20&submit1=Submit the key was implementing the array in the form: in the above page it asks how many disabilities and then builds an array for the form - <select name="percent[]"> Probably not the best example but arrays are easier to work with because you can change the size and use key pairs if needed to extract your data. I know there is a smaller limit on GET than POST but not sure what it is. Quote Link to comment https://forums.phpfreaks.com/topic/231337-best-way-to-pass-a-lot-of-data-with-get/#findComment-1191122 Share on other sites More sharing options...
enridp Posted March 23, 2011 Author Share Posted March 23, 2011 The max size for max browsers's compatibility in GET parameters is near 2KB. That's enough for almost any application but the problem is that 2KB of URL is not very user friendly I guess... You can see another problem with arrays in URLs, the link in your post is not correct because the forum doesn't link all the URL. I think human readability is important too, that's why "friendly and pretty urls" exists. And a URL like this: user%5Bname%5D=Bob+Smith&user%5Bage%5D=47&user%5Bsex%5D=M& doesn't look very friendly. array in ugly urls is the fastest and easiest way, but, is it the best? I don't know Quote Link to comment https://forums.phpfreaks.com/topic/231337-best-way-to-pass-a-lot-of-data-with-get/#findComment-1191153 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.