Get http url parameter without auto decoding using PHP

I have a url like

test.php?x=hello+world&y=%00h%00e%00l%00l%00o 

when i write it to a file

 file_put_contents('x.txt', $_GET['x']); // -->hello world file_put_contents('y.txt', $_GET['y']); // -->\0h\0e\0l\0l\0o 

but I need to write it without encoding

 file_put_contents('x.txt', ????); // -->hello+world file_put_contents('y.txt', ????); // -->%00h%00e%00l%00l%00o 

How can i do this?

thanks

+6
source share
3 answers

Since the supergroups The $_GET and $_REQUEST automatically launched through the decoding function (equivalent to urldecode() ), you just need to return urlencode() data so that it matches the characters passed to the URL String:

 file_put_contents('x.txt', urlencode($_GET['x'])); // -->hello+world file_put_contents('y.txt', urlencode($_GET['y'])); // -->%00h%00e%00l%00l%00o 

I tested this locally and it works great. However, from your comments, you can also look at your encoding settings. If the result of urlencode($_GET['y']) is %5C0h%5C0e%5C0l%5C0l%5C0o , then it turns out that the null character you pass in ( %00 ) is interpreted as the literal string "\0" (for example, \ character concatenated from 0 ) instead of correctly interpreting \0 as a single null character.

You should take a look at the PHP documentation for string coding and ASCII device control characters .

+4
source

You can get unencoded values ​​from the $ _SERVER ["QUERY_STRING"] variable.

 function getNonDecodedParameters() { $a = array(); foreach (explode ("&", $_SERVER["QUERY_STRING"]) as $q) { $p = explode ('=', $q, 2); $a[$p[0]] = isset ($p[1]) ? $p[1] : ''; } return $a; } $input = getNonDecodedParameters(); file_put_contents('x.txt', $input['x']); 
+3
source

I think you can use urlencode() to pass the value to the url and urldecode() to get the value.

0
source

All Articles