Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP requests with multiple query strings

Tags:

url

php

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?

like image 852
user5243421 Avatar asked Sep 25 '12 04:09

user5243421


2 Answers

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.

like image 151
Yes Barry Avatar answered Oct 28 '22 02:10

Yes Barry


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.

like image 37
MarcWan Avatar answered Oct 28 '22 02:10

MarcWan