M Argus Chopin Gyver
M Argus Chopin Gyver

Reputation: 304

$_GET replace some char

I want to send a data via $_GET and the data look like this 7U0e1rK6SmjOJuLyAMc5ZL+EsuGqKo2SRSTq3cQlI20=

and the $_GET variable is voucher_code

When I tried to echo the $_GET['voucher_code'], I always ended up with

7U0e1rK6SmjOJuLyAMc5ZL EsuGqKo2SRSTq3cQlI20= *notice the "+" is replaced by "space"

How to stop the $_GET to do that ? replacing my "+" with "space"

Any reply appreciated

Upvotes: 0

Views: 28

Answers (1)

Kevin Kopf
Kevin Kopf

Reputation: 14195

In order to preserve data you are sending, you have to urlencode it first:

Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the » RFC 3986 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs.

Then, when you receive the data, you have to urldecode it:

var_dump(urlencode('7U0e1rK6SmjOJuLyAMc5ZL+EsuGqKo2SRSTq3cQlI20='));
// result is 7U0e1rK6SmjOJuLyAMc5ZL%2BEsuGqKo2SRSTq3cQlI20%3D
var_dump(urldecode('7U0e1rK6SmjOJuLyAMc5ZL%2BEsuGqKo2SRSTq3cQlI20%3D'));
// result is 7U0e1rK6SmjOJuLyAMc5ZL+EsuGqKo2SRSTq3cQlI20=

Note, that when you receive the data with $_GET, it already passes urldecode.

Upvotes: 1

Related Questions