boo-urns
boo-urns

Reputation: 10376

PHP requests with multiple query strings

I can always test this, but I wanted an official answer from a PHP expert (something I am most certainly not).

If someone makes a request like this:

http://www.somedomain.com/a.php?param1=value1&param2=value2&param3=value3?param1=value4&param2=value5&param3=value6

What kinds of consequences are there when I try to access _GET['param2'], for example? It looks like I just get the first value. In this case, it would return value2. Is that expected behavior?

Does the second question mark affect anything? Will there be errors, for example?

Upvotes: 7

Views: 7398

Answers (2)

MarcWan
MarcWan

Reputation: 2973

You are, officially, not supposed to have two ? in a query string. If you do, the results are undefined. Some platforms (such as PHP) will treat subsequent ? chars the same as & chars, and allow those value/pairs. Others will just treat the value for param3 to be value3?param1=value4.

Short answer: don't do this. It gets wonky. And, as a server author, one should always be validating parameter values carefully to make sure the values make sense.

Upvotes: 2

Yes Barry
Yes Barry

Reputation: 9876

It just gets the last one defined. Defining a parameter more than once in the query string just runs the risk of getting potentially confusing results. It's best to validate your query string in these instances, at least that's what I do.

The second question mark will be treated as part of the value for its preceding parameter.

Upvotes: 3

Related Questions