简体   繁体   中英

url encode behaving differently in Firefox and Internet Explorer

I want to send a user's entry to google's geocode 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. In Internet Explorer however it's not working.

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!)

The same in Internet Explorer is: http://maps.googleapis.com/maps/api/geocode/json?address=k%F6ln+Germany&sensor=false (returns "INVALID_REQUEST")

You can see the difference in the encoding of the ö. In Firefox it's %C3%B6, in IE it's k%F6. If I make the user input "k%C3%B6ln+Germany" to begin with, it works like a charm in Internet Explorer also.

How can I with PHP ensure that the conversion of my special characters is the same in Internet Explorer as in Firefox. So ö = %C3%B6 instead of ö = 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

 <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.

and by the way, you should deliver a html page if you want that to be a link so you should

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

before you echo the <a href=...> and

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

afterwards.

That way IE will use the given UTF-8 encoding on displaying the link.

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.

If you just pass the link to via "AJAX" to a page already loaded make sure that this page contains the mentioned meta-tag.

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. Most web servers check the browser's language settings to detect this setting and use it for localized text generation and parsing. IE's language setting default to those of the operating system user's locale settings.

This means that Firefox may report English as the user's preferred language while IE will report German on the same computer. Unless you specifically convert your input string to UTF8 first, the conversion will depend on the client's locale

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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