使用PHP无需自动解码即可获取http url参数 [英] get http url parameter without auto decoding using PHP
问题描述
我有一个网址,例如
test.php?x = hello + world& y =%00h%00e%00l %00l%00o
当我将它写入文件时
file_put_contents('x.txt',$ _GET ['x']); // - > hello world
file_put_contents('y.txt',$ _GET ['y']); // - && \\\\\\\\\\\\\\\\\\\\\\\\\\\'但我需要写入它,而不编码
file_put_contents('x.txt',????); // - > hello + world
file_put_contents('y.txt',????); // - >%00h%00e%00l%00l%00o
我该怎么办?
谢谢
解决方案 因为 $ _GET
和 $ _ REQUEST
superglobals会自动运行解码函数(相当于 urldecode()$ c $你只需要重新获得 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
我测试过这个在当地进行,而且工作完美。但是,从您的评论中,您可能也想看看您的编码设置。如果 urlencode($ _ GET ['y'])
的结果是%5C0h%5C0e%5C0l%5C0l%5C0o
那么看起来你传入的空字符
(%00
)被解释为文字字符串\0
(如 \ 字符连接到 0 character),而不是正确解释 \0
为单个空字符。
你应该查看 PHP文档字符串编码和ASCII设备控制字符。
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
解决方案 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.
这篇关于使用PHP无需自动解码即可获取http url参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文