获取http url参数而不使用PHP自动解码

use*_*661 7 php get http

我有一个网址

test.php?x=hello+world&y=%00h%00e%00l%00l%00o
Run Code Online (Sandbox Code Playgroud)

当我把它写到文件

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

但我需要写它没有编码

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

我能怎么做?

谢谢

Pau*_*aul 6

您可以从 $_SERVER["QUERY_STRING"] 变量中获取未编码的值。

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']); 
Run Code Online (Sandbox Code Playgroud)

  • 这是正确的答案,因为 Ben D. 建议的重新编码以不同的方式处理某些字符(如斜杠)。 (2认同)

Ben*_*n D 5

因为The $_GET$_REQUESTsuperglobals是通过解码函数(相当于urldecode())自动运行的,所以您只需要重新urlencode()生成数据以使其与URL字符串中传递的字符匹配:

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

我已经在本地进行了测试,它运行良好.但是,根据您的评论,您可能还想查看您的编码设置.如果结果urlencode($_GET['y'])%5C0h%5C0e%5C0l%5C0l%5C0o,则null character表示您传入的(%00)被解释为文字字符串"\0"(如\连接到0字符的字符),而不是正确地将其解释\0为单个空字符.

您应该查看有关字符串编码和ASCII设备控制字符PHP文档.