get http url parameter without auto decoding using PHP

╄→гoц情女王★ 提交于 2020-01-01 05:12:48

问题


I have a url like

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

when i write it to 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 to without encoding

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

how can i do?

Thanks


回答1:


Because the The $_GET and $_REQUEST superglobals are automatically run through a decoding function (equivalent to urldecode()), you simply need to re-urlencode() the data to get it to match the characters passed in 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've tested this out locally and it's working perfectly. However, from your comments, you might want to look at your encoding settings as well. If the result of urlencode($_GET['y']) is %5C0h%5C0e%5C0l%5C0l%5C0o then it appears that the null character that you're passing in (%00) is being interpreted as a literal string "\0" (like a \ character concatenated to a 0 character) instead of correctly interpreting the \0 as a single null character.

You should have a look at the PHP documentation on string encoding and ASCII device control characters.




回答2:


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:


i think you can use urlencode() to pass the value in URL and urldecode() to get the value.



来源:https://stackoverflow.com/questions/15540117/get-http-url-parameter-without-auto-decoding-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!