Jump to content

Best way to pass a lot of data with GET


enridp

Recommended Posts

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :(

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.