Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

php - 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 (e.g. "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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...