JochenJung
JochenJung

Reputation: 7213

UTF-8 Encoding issue in IE query parameters

I have this URL with my test-code: http://www.gruppenunterkuenfte.de/encodingtest.php?q=köln

It passes the query-parameter q to an SQL query.

The strange thing is, if the query is comming from IE, I have to utf8_encode() it. If it comes from Chrome, or Firefox, I may not.

mb_detect_encoding() Always says the query is UTF-8 (IE and Other). The query itselfe however will only work in one of the browsers.

This is my test code from the URL above:

<?php
header('Content-Type: text/html; charset=utf-8');

$config = parse_ini_file("inc/base/config.php", 1);
$link = mysqli_connect($config["database"]["server"], $config["database"]["user"], $config["database"]["passwd"]);
mysqli_select_db($link, $config["database"]["database"]);
mysqli_query($link, "SET NAMES utf8;");

echo 'mb_detect_encoding: '. mb_detect_encoding($_GET['q']) .'<br>';

echo 'Without utf8_encode():<br>';

$res = mysqli_query($link, 'SELECT SQL_CALC_FOUND_ROWS ort FROM BRDOrte
  WHERE ort LIKE \''. addslashes($_GET['q']) .'%\'
  GROUP BY BINARY ort
  ORDER BY BINARY ort
  LIMIT 5
  ');
while ($row = mysqli_fetch_array($res)) print_r($row);


echo '<br>With utf8_encode():<br>';

$res = mysqli_query($link, 'SELECT SQL_CALC_FOUND_ROWS ort FROM BRDOrte
  WHERE ort LIKE \''. utf8_encode(addslashes($_GET['q'])) .'%\'
  GROUP BY BINARY ort
  ORDER BY BINARY ort
  LIMIT 5
  ');
while ($row = mysqli_fetch_array($res)) print_r($row);

?>

The MySQL table and all its fields are utf8_general_ci.

So how do I tell IE to send the Query parameter as UTF-8?

Or how do I detect it on server side, so I can encode the right way?

Upvotes: 1

Views: 6825

Answers (3)

Chris Baker
Chris Baker

Reputation: 50592

The generic syntax of a URI (RFC3986) requires that any characters not 7-bit ASCII are to be percent-encoded (URI Encoded). When you operate outside that standard, user agent handling will vary unpredictably. You will either have to a) encode the URL string before sending it with javascript (see encodeURIComponent), b) encode it during page render with PHP (see urlencode), or c) create a ternary statement in the request handler to deal with either situation:

$tag = strlen($_GET['q']) > 1 ? $_GET['q'] : utf8_encode($_GET['q']);

Upvotes: 5

mustafa
mustafa

Reputation: 745

May be you can try something on server side

check and edit /etc/apache2/conf.d/charset ( Your server may have different directory) and add lines beolow. I realized you use some Dutch characters, so you may add character for that language also.

AddDefaultCharset UTF-8
AddDefaultCharset iso-8859-9

Upvotes: -1

Yasen Zhelev
Yasen Zhelev

Reputation: 4045

Use urlencode to pass the query parameter to the browser. It will become k%F6ln Then you should be able to read it back with urldecode.

For JavaScript try encodeURIComponent or its alternatives

Upvotes: 3

Related Questions