1

So I have the following PHP-code:

echo $_GET["r"];

Which is really simple..

But when I request the PHP-file like this: ....php?r=123+123, it spits out "123 123", not "123+123" with a plus sign. Why is this happening and how do I fix it?

I have tried:

echo urlencode($_GET["r"]);

Which works, BUT, this was a really simple example, if I were to set that to a variable:

$r = urlencode($_GET["r"]);

The variable $r would be set to 123%2B123 and not 123+123, I want the variable $r to be set with the real plus sign, not %2B?

3 Answers3

1

The Plus sign is a reseverd character in URLs. When using it directly calling a php script it will result in space character.

When you need to send a plus sign, there is no way around url encoding like suggested in the comments already.

mblaettermann
  • 1,916
  • 2
  • 17
  • 23
1

So, I found it, instead of urlencode, I just used RAWurlencode!

1

It is correct that the URL php?r=123+123 is decoded as "123 123" separated by spaces, not as plus signs. To have it correctly decoded, make it encoded as php?r=123%2B123. Even if you wrote that you do not wish so, please consider this correct behavior.

Space characters are encoded as "+" in application/x-www-form-urlencoded key-value pairs.

The RFC-1866 (HTML 2.0 specification), paragraph 8.2.1. subparagraph 1. says: "The form field names and values are escaped: space characters are replaced by `+', and then reserved characters are escaped").

Here is an example of such a string in URL where RFC-1866 allows encoding spaces as pluses: "http://example.com/over/there?name=foo+bar". So after "?", spaces are replaced by pluses. This way of encoding form data is also given in later HTML specifications, for example, look for relevant paragraphs about application/x-www-form-urlencoded in HTML 4.01 Specification, and so on.

As about the urlencode and rawurlencode, please find more information at urlencode vs rawurlencode?

Community
  • 1
  • 1
Maxim Masiutin
  • 3,991
  • 4
  • 55
  • 72