Forum Moderators: coopster

Message Too Old, No Replies

How to not pass GET through urldecode()?

Stop PHP pass all GET variables automatically through urldecode()

         

yaix2

3:55 am on Oct 22, 2010 (gmt 0)

10+ Year Member



How to not automatically pass $_GET through urldecode(), is there a way?

I have a $_GET['abc'] variable that sometimes has '+' in it's value, and the value should remain as-is. But PHP passes all GET variables automatically through urldecode() and converts the pluses into spaces.

How can I make PHP not using urldecode automatically? It would be OK to use rawurldecode() that actually folows RFC1738.

enigma1

8:51 am on Oct 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are custom ways. PHP parse_ functions are pretty useless so you need to setup your own and patch $_GET.

<?php
// Snip taken from the I-Metrics CMS
function tep_get_string_parameters($string) {
$result = array();
if( empty($string) || !is_string($string) ) return $result;
$string = str_replace('&amp;', '&', $string);
$params_array = explode('&', $string);
foreach($params_array as $value) {
$tmp_array = explode('=', $value);
if( count($tmp_array) != 2) continue;
$result[$tmp_array[0]] = $tmp_array[1];
}
return $result;
}

// Lets use the query string directly from the server var and override the super-global
$_GET = tep_get_string_parameters($_SERVER['QUERY_STRING']);
print_r($_GET);
?>

yaix2

1:20 am on Oct 23, 2010 (gmt 0)

10+ Year Member



Thanks enigma1 for your help. I thought there was maybe some "switch" to tell PHP how to handle GET, but simply parsing it myself should work too. Thanks again!