Search code examples
javascriptphpcharacter-encodingchars

$_GET vs $_SERVER['QUERY_STRING'] - encode issue


I'm accessing my script throw an URL like that

http://www.anydomain.com/code.php?a=ií

and code.php is basically this

<?php header('Content-Type: text/html; charset=utf-8'); ?>
<html><head>
    <meta charset="UTF-8">
</head><body>
<script>
    console.log('<?php echo $_SERVER['QUERY_STRING'];?>');
    console.log(unescape('<?php echo $_SERVER['QUERY_STRING'];?>'));
    console.log('<?php echo $_GET['a'];?>');
</script>
</body></html>

the logs I'm getting are:

a=i%C3%AD
a=ií

In real life I'm expecting a lot of parameters and need to save the full query, so $_SERVER['QUERY_STRING'] would be very handy. But the charset seems not to be working properly, as it is with the $_GET function.

Is there a way to get the query using $_SERVER['QUERY_STRING'] but using the charset of $_GET?


Solution

  • The problem was not at PHP at all but in the javascript function unescape. When I changed to decodeURIComponent it worked just fine.