Skip to content
Home » Web » PHP » How to Use $_SERVER['QUERY_STRING'] Properly

How to Use $_SERVER['QUERY_STRING'] Properly

$_SERVER['QUERY_STRING']

Let's see a snippet of PHP code, which will generate three similar output:

<?php
echo $_SERVER['QUERY_STRING'] . "<br />";
echo urldecode($_SERVER['QUERY_STRING']) . "<br />";
echo $_GET['a'];
?>

Then, we test the page by clicking the URL with a query string containing two multibyte words.

http://localhost/test.php?a=京都

The output is:

a=%E4%BA%AC%E9%83%BD
a=京都
京都

urldecode

As you can see, $_GET is already url-decoded, but $_SERVER['QUERY_STRING'] is not. Therefore, if you insist to use $_SERVER['QUERY_STRING'], you have to urldecode() the string before using it. Otherwise, you will meet unexplained errors in multilingual websites.

Leave a Reply

Your email address will not be published. Required fields are marked *