免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6923 | 回复: 7

关于URL参数传递乱码的问题 [复制链接]

论坛徽章:
0
发表于 2006-09-07 13:57 |显示全部楼层
在win2003下,安装了apache2.2 ,perl 5.8.8。

当我在 URL 中传递一个参数,如: test.cgi?title=我的测试
接收到的参数为乱码

谁能告诉我怎么修改啊!

谢谢

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2006-09-07 14:10 |显示全部楼层
不是乱码,是 URL 编码。你需要解码。
看看这个:
http://search.cpan.org/~gaas/URI-1.35/URI/Escape.pm

论坛徽章:
0
发表于 2006-09-07 14:23 |显示全部楼层
请问怎么转回中文?

  1. #!/usr/bin/perl

  2. use CGI qw(:standard);
  3. use URI::Escape;
  4. use Encode qw(encode decode);

  5. print "Content-Type:text/html \n\n";
  6. my $in = new CGI();

  7. my $t = $in->param('t');
  8. #$t = uri_escape(encode("GB2312",$t));
  9. #$t =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  10. #$t = encode("GB2312",$t);
  11. print $t;
复制代码


当 调用 test.cgi?t=我的站点
时是乱的

就是说 我在参数传递的时候不修改,接受参数的时候 是否可以转回来?

[ 本帖最后由 yhsmengdi 于 2006-9-7 14:31 编辑 ]

论坛徽章:
0
发表于 2006-09-07 15:12 |显示全部楼层
flw 大师再帮我看看

论坛徽章:
0
发表于 2006-09-07 17:21 |显示全部楼层
原帖由 yhsmengdi 于 2006-9-7 13:57 发表
在win2003下,安装了apache2.2 ,perl 5.8.8。

当我在 URL 中传递一个参数,如: test.cgi?title=我的测试
接收到的参数为乱码

谁能告诉我怎么修改啊!

谢谢


Hallo,

Apache will automatically encoded the query string "title=我的测试" as URI-Encoding
string as following:
$ENV{QUERY_STRING} =  title=%E6%88%91%E7%9A%84%E6%B5%8B%E8%AF%95
using the method uri_unescape from URI::Escape to decode the querystring, as default
the decode string is UTF-8 fomat.
In order to display in chinese, you must use Encode::decode('UTF-8', STRING) and then
Encode::encode('GB2312', STRING) again.

here is demo code test.cgi again:
and point the browser to /cgi-bin/test.cgi?title=我的测试

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use URI::Escape;
  4. use Encode;

  5. my $qstr = '';
  6. my $unescaped_qstr = '';
  7. print qq(Content-type: text/html; charset=GB2312);
  8. print qq(\n\n);
  9. if ($ENV{'QUERY_STRING'}) {
  10.     $qstr = $ENV{'QUERY_STRING'};
  11.     $unescaped_qstr = uri_unescape($qstr);
  12.     print "query: uri-encoded: $qstr<br>\n";
  13.     print "uri-unescaped as utf-8: $unescaped_qstr<br>\n";
  14.     print "show GB2312:", encode('GB2312', decode("UTF-8", $unescaped_qstr)), "<br>\n";
  15. }
  16. else {
  17.     print "no query string<br>\n";
  18. }

复制代码


regards, ulmer

论坛徽章:
0
发表于 2006-09-11 15:37 |显示全部楼层

好贴,多谢楼上的兄弟!!!

好贴,多谢楼上的兄弟!!!

论坛徽章:
0
发表于 2008-07-16 16:38 |显示全部楼层
遇到同样问题,尚未解决,在一帖子里apile老大说是十六进制,不知道怎么转换成字符串?

论坛徽章:
0
发表于 2008-07-20 23:57 |显示全部楼层
原帖由 ulmer 于 2006-9-7 17:21 发表


Hallo,

Apache will automatically encoded the query string "title=我的测试" as URI-Encoding
string as following:
$ENV{QUERY_STRING} =  title=%E6%88%91%E7%9A%84%E6%B5%8B%E8%AF%95

using th ...


麻烦问下大哥

我是用的Apache2.0,为什么没有自动转换呢?
出现的是"http://localhost/cgi-bin/article/article.cgi?type=栏目ID1"

为什么还是汉字???

是有什么地方需要设置吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP