Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to preserve plus signs in PHP $_GET vars without encoding?

Tags:

php

encoding

get

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!

like image 665
user353885 Avatar asked Aug 16 '12 00:08

user353885


People also ask

How to use urlencode in PHP?

PHP | urlencode() Function. The urlencode() function is an inbuilt function in PHP which is used to encode the url. This function returns a string which consist all non-alphanumeric characters except -_. and replace by the percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.

How do you check if a URL is encoded?

So you can test if the string contains a colon, if not, urldecode it, and if that string contains a colon, the original string was url encoded, if not, check if the strings are different and if so, urldecode again and if not, it is not a valid URI.

How do I encode a URL in href?

URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

How do I pass an email address into a URL?

Now, whenever you have to use the email address from the URL, simply do the following: $decodedEmail = urldecode($_GET['email']); // It will return [email protected] as a string, decoded. Hope it answers your question.


1 Answers

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.

preg_match_all('/(\w+)=([^&]+)/', $_SERVER["QUERY_STRING"], $pairs);
$_GET = array_combine($pairs[1], $pairs[2]);

(Example only works for alphanumeric parameters, and doesn't do the mentioned %xx decoding. Just breaks up the raw input.)

like image 109
mario Avatar answered Sep 29 '22 11:09

mario