简体   繁体   English

url编码在Firefox和Internet Explorer中的行为有所不同

[英]url encode behaving differently in Firefox and Internet Explorer

I want to send a user's entry to google's geocode API. 我想将用户的条目发送到谷歌的地理编码API。 In doing so, I detected a problem. 这样做,我发现了一个问题。 When I send the user input (eg "köln+Germany") through my script to the api in Firefox it works great. 当我通过我的脚本将用户输入(例如“köln+ Germany”)发送到Firefox中的api时,效果很好。 In Internet Explorer however it's not working. 但是在Internet Explorer中,它无法正常工作。

Here's the exempt of my code that's enough to show the problem: 这是我的代码的豁免,足以显示问题:

header('Content-type: text/html; charset=UTF-8');
header('Cache-Control: no-cache, must-revalidate');
$loc = urlencode($_GET['loc']);
echo $address = "http://maps.googleapis.com/maps/api/geocode/json?address=$loc&sensor=false";

The output ($address) in Firefox is: http://maps.googleapis.com/maps/api/geocode/json?address=k%C3%B6ln+Germany&sensor=false (works!) Firefox中的输出($ address)是: http//maps.googleapis.com/maps/api/geocode/json? address = k%C3%B6ln + Germany&_sensor = false (有效!)

The same in Internet Explorer is: http://maps.googleapis.com/maps/api/geocode/json?address=k%F6ln+Germany&sensor=false (returns "INVALID_REQUEST") Internet Explorer中的相同之处是: http ://maps.googleapis.com/maps/api/geocode/json?address=k%F6ln+Germany&sensor=false(返回“ INVALID_REQUEST”)

You can see the difference in the encoding of the ö. 您会看到ö编码的差异。 In Firefox it's %C3%B6, in IE it's k%F6. 在Firefox中它是%C3%B6,在IE中它是k%F6。 If I make the user input "k%C3%B6ln+Germany" to begin with, it works like a charm in Internet Explorer also. 如果我开始用户输入“k%C3%B6ln +德国”,它就像在Internet Explorer中的魅力一样。

How can I with PHP ensure that the conversion of my special characters is the same in Internet Explorer as in Firefox. 我如何使用PHP确保Internet Explorer中的特殊字符转换与Firefox中的相同。 So ö = %C3%B6 instead of ö = k%F6 所以ö=%C3%B6而不是ö= k%F6

Thank you very much! 非常感谢你!

Paul 保罗

This is an encoding Problem, the default encoding in Firefox is UTF-8 while in IE it is some ISO-XXXX-X, try to set 这是一个编码问题,Firefox中的默认编码是UTF-8,而在IE中则是一些ISO-XXXX-X,尝试设置

 <meta http-equiv="content-type" content="text/html; charset=UTF-8">

in Your HTML - <head>...</head> this will set the encoding to UTF-8, so IE will urlencode the string like Firefox did and therefore produces a working request. 在您的HTML中 - <head>...</head>这会将编码设置为UTF-8,因此IE会像Firefox一样对该字符串进行urlencode,从而产生一个工作请求。

and by the way, you should deliver a html page if you want that to be a link so you should 顺便说一句,如果您希望将其作为链接,则应该提供一个html页面,因此您应该

echo  '<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body>';

before you echo the <a href=...> and 在回显<a href=...>之前

echo '</body></html>';

afterwards. 然后。

That way IE will use the given UTF-8 encoding on displaying the link. 这样,IE将在显示链接时使用给定的UTF-8编码。

It might seem as if it would work without that stuff, butt then IE decodes the given <a href=...> , guesses it is a link and then makes his own html-head-body around it to display it - which then includes a <meta http-equiv="content-type" content="text/html; charset=ISO-XXXX-X"> instead. 看起来好像没有那些东西就可以工作,然后IE解码给定的<a href=...> ,猜测它是一个链接,然后在它周围制作自己的html-head-body来显示它 - 然后包括<meta http-equiv="content-type" content="text/html; charset=ISO-XXXX-X">

If you just pass the link to via "AJAX" to a page already loaded make sure that this page contains the mentioned meta-tag. 如果您只是通过“ AJAX”将链接传递到已经加载的页面,请确保该页面包含提到的元标记。

The problem is only indirectly related to the browser. 该问题仅与浏览器间接相关。 urlencode treats strings as a sequence of bytes and as a result is affected by the locale used by the server page. urlencode将字符串视为字节序列,结果受服务器页面使用的语言环境影响。 Most web servers check the browser's language settings to detect this setting and use it for localized text generation and parsing. 大多数Web服务器检查浏览器的语言设置以检测此设置,并将其用于本地化文本生成和解析。 IE's language setting default to those of the operating system user's locale settings. IE的语言设置默认为操作系统用户的语言环境设置。

This means that Firefox may report English as the user's preferred language while IE will report German on the same computer. 这意味着Firefox可能会报告英语作为用户的首选语言,而IE会在同一台计算机上报告德语。 Unless you specifically convert your input string to UTF8 first, the conversion will depend on the client's locale 除非首先将输入字符串专门转换为UTF8,否则转换将取决于客户端的语言环境

So I know this can't be the proper solution, but for anyone running into the same problem, this workaround worked for me: 所以我知道这不是一个合适的解决方案,但对于遇到同样问题的人来说,这个解决方法对我有用:

$loc = $_GET['loc'];
$loc = utf8_encode($loc); 
$trans = array("ä" => "ae", "Ä" => "Ae", "ü" => "ue", "Ü" => "Ue", "ö" => "oe", "Ö" => "Oe", "ß" => "ss");
$loc = strtr($loc, $trans);
$loc = urlencode($loc);

Since I don't need to preserve the Umlauts in my context this is ok. 由于我不需要在上下文中保留Umlauts,所以可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM