开发者

Handling unicode values in GET parameters with PHP

开发者 https://www.devze.com 2022-12-19 03:16 出处:网络
I have the following开发者_开发百科 test script on my server: <?php echo \"Test is: \" . $_GET[\'test\'];

I have the following开发者_开发百科 test script on my server:

<?php
echo "Test is: " . $_GET['test'];
?>

If I call it with a url like example.com/script.php?test=ɿ (ɿ being a multibyte character), the resulting page looks like this:

Test is: É¿

If I try to do anything with the value in $_GET['test'], such as save it a mysql database, I have the same problem. What do I need to to do make PHP handle this value correctly?


Have you told the user agent your HTTP response is UTF-8?

header ('Content-type: text/html; charset=utf-8');

You might also want to ensure your HTML markup declares the encoding also, e.g.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

For your database, are your tables and mysql client settings set up for UTF-8? If you check your database using a mysql command line client, is your terminal environment set up to expect UTF-8?

In a nutshell, you must check every step: from the raw source data, the code which touches it, the storage systems which retain it, and the tools you use to display and debug it.


UTF-8 all the way through…


Follow the steps, specifically:

  • SET NAMES 'utf8' upon connection to the MySQL DB
  • <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> in your HTML


By pasting url in browser which cotains high utf8 chars, browser will recode utf8 chars into a multibyte sequence compliant with RFC 3986 and you won't get utf8 chars in php.

BUT, php will get and display utf8 chars from url correctly, if page which calls your url is utf8 encoded.

Try calling your php for test like this:

<iframe src="example.com/script.php?test=ɿ" height="100" width="100" border="1"></iframe>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号