0

My $_SERVER['QUERY_STRING'] returns this:

route=common/home/test/aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g%2Fdj1iZWsxeTJ1aVFHQQ%3D%3D/456&param2=test

As we can see that there is base64 encoded string passed with get request. I want to parse route parameter without decoding its value.

I need this one:

common/home/test/aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g%2Fdj1iZWsxeTJ1aVFHQQ%3D%3D/456

Not this one:

common/home/test/aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1iZWsxeTJ1aVFHQQ==/456

I tried to parse route parameter with parse_str function. But it decoded the route's value.

  • So are you saying you want the content of route passing to a new variable with the encoding intact? – FoxyFish Jul 01 '18 at 17:31
  • Either reencode it then, or use a regex to [split it up](https://stackoverflow.com/questions/5290342/php-split-delimited-string-into-key-value-pairs-associative-array). – mario Jul 01 '18 at 17:38
  • If literally interpreting the requirements from the lone sample string and there will only be two pairs of data, [this answer could be easily adjusted](https://stackoverflow.com/a/72951413/2943403). – mickmackusa Jul 12 '22 at 13:20

2 Answers2

1
$route_var = $_REQUEST["route"];
$pieces = explode("/", $route_var);
$sliced = array_slice($pieces, 3)
$based = implode("/", $sliced);
$pathslice = array_slice($pieces, 0,3);
$path = implode("/", $pathslice);
$b64part = urlencode($based);
$output = $path."/".$b64part;

This should give you your correct encoded base64 if you echo output.

FoxyFish
  • 874
  • 1
  • 12
  • 21
  • I need dynamic way. Also your code returned: `aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g` but I need `aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g%2Fdj1iZWsxeTJ1aVFHQQ%3D%3D` – user2398907 Jul 01 '18 at 18:06
0

You could essentially replicate parse_str but without applying urldecode:

$x = $_SERVER['QUERY_STRING'];
$y = explode('&', $x);

$qs = [];

foreach($y AS $z) {
    list($key, $val) = explode('=', $z);
    $qs[$key] = $val;
}

Which should give you

array(2) {
  ["route"]=> string "common/home/test/aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g%2Fdj1iZWsxeTJ1aVFHQQ%3D%3D/456"
  ["param2"]=> string "test"
}
MC57
  • 329
  • 1
  • 5