• 问题:不同网站的跳转出现乱码,不同编码的页面传递参数也出现乱码

    搞清楚两个问题:

    1. URL文字本身的编码, 一般与你网页的编码一致;中文一般才用UTF-8、GBK、GB2312
    2. 什么是URL编码

    URL的编码规则

    • 大小写字母,数字不变
    • “.”, “-”, “*”, “_” 四个字符不变
    • 空格键编码为加号"+"
    • 其它所有字符被视为不安全字符,按所指定的编码方式编码(如果未指定则为默认为平台编码,浏览器或操作系统决定),以每字节十六进制形式表示出来,具体格式为”%xy”.xy为两个十六进制数,用来描述一个8位的字节.
    • RFC1738协议

    OK,无论网站使用什么平台,URL编码规则,都是一致的,所以不同网站进行页面跳转时,出现乱码问题,就是网站使用的编码不一致所引起的。

    URL解码-> 转换字符串的编码->URL编码

    一、PHP的解决方案:

    PHP字符串编码转换函数

    iconv() 函数

    Description string iconv ( string in_charset, string out_charset, string str )

    注意:第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀:

    1. //TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符;
    2. //IGNORE 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。
     eg:$str = iconv("UTF-8","GB2312//TRANSLIT",$str);

    mb_convert_encoding() 函数

    Description string mb_convert_encoding ( string str, string to-encoding [, mixed from-encoding])

    注意:需要enable mbstring 扩展库。

    两者区别:mb_convert_encoding 中根据内容自动识别编码;mb_convert_encoding功能强大,但是执行效率比iconv差太多;

    总结:一般情况下用 iconv,只有当遇到无法确定原编码是何种编码时用 mb_convert_encoding 函数.

    URL编码解码函数

    urlencode函数
    该函数将传入的字符串参数进行URL编码。声明如下:

    string urlencode (string str)

    urlencode函数
    该函数将传入的字符串参数进行URL解码,返回解码后的字符串。声明如下:

    string urldecode (string str)

    其他的
    rawurlencode()
    rawurldecode()

    实例:

    <?PHP
    $url=$_GET['url'];
    $url = htmlspecialchars(urldecode($url));
    $keyword = iconv("UTF-8","GB2312//TRANSLIT",$url);
    $keyword = urlencode($url);
    header("Location: " . $url);
    ?>

    二、JavaScript的解决方案

    在使用url进行参数传递时,经常会传递一些中文名的参数或URL地址,在后台处理时会发生转换错误。在有些传递页面使用GB2312,而在接收页面使用 UTF8,这样接收到的参数就可能会与原来发生不一致。使用服务器端的urlEncode函数编码的URL,与使用客户端javascript的 encodeURI函数编码的URL,结果就不一样。

    javaScript中的编码方法:

    escape() 方法:
    采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符 在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。unescape方法与此相反。不会被此方法编码的字符: @ * / +

    英文解释:MSDN JScript Reference: The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as “%20.”
    Edge Core Javascript Guide: The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII string for the specified hexadecimal encoding value.

    encodeURI() 方法:

    把URI字符串采用UTF-8编码格式转化成escape格式的字符串。不会被此方法编码的字符:! @ # $& * ( ) = : / ; ? + ‘

    英文解释:MSDN JScript Reference: The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned. The encodeURI method does not encode the following characters: “:”, “/”, “;”, and “?”. Use encodeURIComponent to encode these characters. Edge Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character

    encodeURIComponent() 方法:

    把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比,这个方法将对更多的字符进行编码,比如 / 等字符。所以如果字符串里面包含了URI的几个部分的话,不能用这个方法来进行编码,否则 / 字符被编码之后URL将显示错误。不会被此方法编码的字符:! * ( )

    英文解释:MSDN JScript Reference: The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1/folder2/default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a single URI component. Mozilla Developer Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.
    因 此,对于中文字符串来说,如果不希望把字符串编码格式转化成UTF-8格式的(比如原页面和目标页面的charset是一致的时候),只需要使用 escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用encodeURI或者 encodeURIComponent。

    另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。

    英文注释:The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. Due to this shortcoming, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs [REF] as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method  does not encode the ‘ character, as it is a valid character within URIs.Lastly, the encodeURIComponent() method should be used in most cases when encoding a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included. Note that this method does not encode the ‘ character, as it is a valid character within URIs.

    三、jsp、servlet的解决方案

     
    在Servlet中,一般有参数传递的话,会设置页面接收参数和传递参数的编码。即下面两句:
     
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("utf-8");
     
    一般情况下,大部分都会想到使用这个,但是这两句代码的位置有时却容易被忽视。正确的写法是,request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("utf-8");要放在
     
    PrintWriter out = response.getWriter();的后面。因为out对象初始化之后,再设置编码已经没有任何意义了!所以必须在out对象初始化之前进行编码的设置。

关于header跳转之后的乱码的更多相关文章

  1. 关于header跳转之后的乱码问题

    关于header跳转之后的乱码问题 http://www.360doc.com/content/11/0603/19/7052474_121495648.shtml 问题:不同网站的跳转出现乱码,不同 ...

  2. location跳转和header跳转的区别

    1:header("location:url") 跳转之前不能有任何输出,如果想在header之前有输出,则要修改php.ini文件.具体 output_handler =mb_o ...

  3. php header()跳转

    test1.php <?PHP $g_user = "Jack"; echo $g_user; ?> test3.php <?PHP header('Locati ...

  4. header 跳转时报错误。Header may not contain more than a single header, new line detected

    我在用php的header做跳转时,报错误. Header may not contain more than a single header, new line detected 先贴一下代码: c ...

  5. header()跳转

    if ($toNews == 1) { header('Location:/ucenter/pageMailBox/2'); exit; } PHP跳转页面,用 header() 函数 定义和用法 h ...

  6. thinkPHP5.0中使用header跳转没作用

    我在controller中的方法中这样写: header("Location:".$url); 但是一直没动静,不会跳转,最后还是官方文档解决了 https://www.kancl ...

  7. html页面跳转出现中文乱码

    一般html的<meta charset="UTF-8">默认都是utf-8的编码格式,直接在网页中打开是正常的,但是有的时候在页面跳转时就会出现中文乱码问题,忘了参考 ...

  8. js location.href 和 php header 跳转方式

    if ($result){ echo '添加成功';// header('refresh:3,url=user_nameList.php'); echo "<script> al ...

  9. php 两次encodeURI,解决浏览器跳转请求页乱码报错找不到页面的bug

    Not Found The requested URL /index.php/XXX/mid/97329240798095910/bname/3000T/D/sname/水泥粉磨/un ...

随机推荐

  1. osg矩阵变换节点-----平移旋转缩放

    osg矩阵变换节点-----平移旋转缩放 转自:http://www.cnblogs.com/ylwn817/articles/1973396.html 平移旋转缩放这个三个是osg矩阵操作中,最常见 ...

  2. Java抽象类与接口的区别

    很多常见的面试题都会出诸如抽象类和接口有什么区别,什么情况下会使用抽象类和什么情况你会使用接口这样的问题.本文我们将仔细讨论这些话题. 在讨论它们之间的不同点之前,我们先看看抽象类.接口各自的特性. ...

  3. tomcat配置https

    1.开启使用https协议 编辑tomcat目录下的conf/server.xml文件 <Connector port="443" protocol="HTTP/1 ...

  4. 青蛙跳100级台阶算法,完整可运行,php版本

    /* 算法题目 * 2016年4月11日16:11:08 * 一只青蛙,一次可以跳1步,或者2步,或者3步,现在要跳100级台阶,请问青蛙有多少种上100级台阶的跳法 * 1步的有$n 2步的有$m ...

  5. JMeter--集合点设置(转)

    集合点:简单来理解一下,虽然我们的"性能测试"理解为"多用户并发测试",但真正的并发是不存在的,为了更真实的实现并发这感念,我们可以在需要压力的地方设置集合点, ...

  6. oracle分组取第一条

    SELECT * FROM (SELECT ROW_NUMBER() OVER(PARTITION BY x ORDER BY y DESC) rn,test1.* FROM test1) WHERE ...

  7. Xcode清除缓存等

    Xcode出现一些错误的时候,有时候不是代码的问题,需要清理一下Xcode的缓存或者项目的Product等: 1. Product清理 1.1  Product-Clean 1.2  Product- ...

  8. idea 光标变成粗体且当前文件搜索功能无法使用的问题

    今天安装了idea最新版,安装完成后发现光标变成了粗体,并且快捷键在使用时出现了问题,比如:ctrl+F搜索功能无法使用 经过反复修改配置也无法改善情况,后来一次重启看到下面小窗弹出有关vim的一个提 ...

  9. Python学习【第七篇】基本数据类型

    基本数据类型 数字 2是一个整数的例子. 长整数 不过是大一些的整数. 3.23和52.3E-4是浮点数的例子,E标记表示10的幂.在这里,52.3E-4表示52.3*10-4. (-5+4j)和(2 ...

  10. Windows Server 2008 R2 域控服务器运行nslookup命令默认服务器显示 UnKnown

    一.问题: 域控服务器DOS窗口运行nslookup命令提示如下: 二.原因分析: 主要原因在于域控服务器的DNS服务器没有设置反向查找区域,计算机名称是通过IP地址反向查找到域控服务器的计算机名称. ...