Say I request this URL:
http://mydomain.com/script.php?var=2+2
$_GET['var'] will now be: "2 2" where it should be "2+2"
Obviously I could encode the data before sending and then decode it, but I'm wondering if this is the only solution. I could also replace spaces with plus symbols, but I want to allow spaces as well. I simply want whatever characters were passed, without any url decoding or encoding going on. Thank you!
For future reference you can make the '+' symbol appear on a get request without the need submit the url with encoded symbols.
Submitting http://mydomain.com/script.php?var=2+2
Echo:
Using the following code:
You could use
urlencode
, though that would also translate any spaces to a plus. This makes sense, because a+
in a URL usually represents a space. If you actually need a plus sign to mean a plus sign, you should probably escape the input. This means a+
would become%2B
and your URL would behttp://mydomain.com/script.php?var=2%2B2
.A few steps required to do it.
Just replace your query parameter plus (+) sign with '%2B' in the url before requesting the url.
Now you will just retrieve the query parameter. It will automatically replace '%2B' with plus sign.
I just used
urlencode()
to - well - encode the URL andurldecode($_Get())
to make the string usable again.Of course. You could read out
$_SERVER["QUERY_STRING"]
, break it up yourself, and then forgo the usual URL decoding, or only convert%xx
placeholders back.(Example only works for alphanumeric parameters, and doesn't do the mentioned %xx decoding. Just breaks up the raw input.)