- 论坛徽章:
- 0
|
如何正确取得从其他网站get过来的utf8 编码的变量?
嘿,还真是麻烦呀。折腾我大半天。
先说结论 apache+php+windows 在以get方式接收未经过url编码的汉字变量时有可能出错。以往我的开发都在Linux下做,而且总是使用urlencode来编码所以忽略了这个问题。
我把你的测试代码修改了一下同时放在两个服务器上作了试验。
192.168.0.52 Windows XP
192.168.0.51 Linux
send8.php
- <html>;
- <head>;
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">;
- <title>;test</title>;
- </head>;
- <body>;
- <?
- function showResult($server,$getString)
- {
-
- $fp=fopen($server.$getString,"r");
- while (!feof ($fp)) {
- $buffer = fgets($fp, 4096);
- echo $buffer;
- }
- fclose ($fp);
- }
- $linuxServer ='http://192.168.0.51/question/';
- $winServer = 'http://192.168.0.52/question/';
- $sender ="傻瓜";
- $qString = "receiver8.php?mobile=13691&month=8&srcmobile=13691&sender=$sender&svid=test&msg=请帮我查一下本辆摩托车的真假&uid=ZQ1315&password=ZQ1315";
- //query the receiver8.php at linux server.
- echo('Result from linux: <br>;');
- showResult($linuxServer,$qString);
- echo('<hr>;');
- //query the receiver8.php at win server.
- echo('Result from win: <br>;');
- showResult($winServer,$qString);
- ?>;
- </body>;
- </html>;
复制代码
receiver8.php
- <html>;
- <head>;
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">;
- <title>;test</title>;
- </head>;
- <body>;
- <?
- $fp = fopen('test.txt','wb');
- //$msg=urldecode(urlencode($_ENV["QUERY_STRING"]));
- $params = split('&',$_SERVER["QUERY_STRING"]);
- var_dump($params);
- if($fp)
- {
- flock($fp, 3);
- foreach ($params as $key=>;$param) {
- fwrite($fp,$param."\n") or die('写文件错误');
- }
- flock($fp, 1);
- }
- ?>;
- </body>;
- </html>;
复制代码
建议方案:
1、如果要在程序里传递url变量一定要用urlencode编码,否则后患无穷,子孙后代将遭人强烈鄙视。
2、建议使用linux服务器环境。 |
|