IE浏览器用GET方式传递中文字符出现乱码问题的解决方法

时间:2022-01-03 06:39:44

昨天编码发现GET方式传递中文字符,后台接收之后显示乱码,换了chrome和Firefox浏览器均无此现象。经网络多方查询,分析是IE浏览器传递参数时采用编码为gb2312,而后台编码格式为UTF8,因此出现乱码。

综合网上多位同学的方案,最后采用如下方式得以解决:(PHP语言). getBrowser函数为网络搜索得到,忘记出处无法注明,见谅。

        $browser = getBrowser();
        if ($browser[0] == "Internet Explorer" ) {
    $xx= iconv("gb2312","utf-8", $xx);
        // 或者 $xx= mb_convert_encoding($xx,"utf-8", "gb2312");  
        }

//查询浏览器类型,返回的数组第一个为浏览器名称,第二个是版本号。
function getBrowser(){
$sys = $_SERVER['HTTP_USER_AGENT'];
if(stripos($sys, "NetCaptor") > 0){
  $exp[0] = "NetCaptor";
  $exp[1] = "";
}elseif(stripos($sys, "Firefox/") > 0){
  preg_match("/Firefox\/([^;)]+)+/i", $sys, $b);
  $exp[0] = "Mozilla Firefox";
  $exp[1] = $b[1];
}elseif(stripos($sys, "MAXTHON") > 0){
  preg_match("/MAXTHON\s+([^;)]+)+/i", $sys, $b);
  preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie);
 // $exp = $b[0]." (IE".$ie[1].")";
  $exp[0] = $b[0]." (IE".$ie[1].")";
  $exp[1] = $ie[1];
}elseif(stripos($sys, "MSIE") > 0){
  preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie);
  //$exp = "Internet Explorer ".$ie[1];
  $exp[0] = "Internet Explorer";
  $exp[1] = $ie[1];
}elseif(stripos($sys, "Netscape") > 0){
  $exp[0] = "Netscape";
  $exp[1] = "";
}elseif(stripos($sys, "Opera") > 0){
  $exp[0] = "Opera";
  $exp[1] = "";
}elseif(stripos($sys, "Chrome") > 0){
   $exp[0] = "Chrome";
   $exp[1] = "";
}else{
  $exp = "未知浏览器";
  $exp[1] = "";
}
return $exp;
}