使用PHP处理GET参数中的unicode值 [英] Handling unicode values in GET parameters with PHP

查看:184
本文介绍了使用PHP处理GET参数中的unicode值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器上有以下测试脚本:

 <?php 
echo测试是: 。 $ _GET [测试];
?>

如果我使用像 example.com/script.php ?test =ɿ(ɿ为多字节字符),结果页面如下所示:


测试是:É¿


如果我尝试用$ _GET ['test']中的值做任何事情,比如保存一个mysql数据库,我也有同样的问题。您需要做什么才能使PHP正确处理此值?

解决方案

您是否告知用户代理您的HTTP响应是UTF -8?

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

您也可以确保HTML标记也声明编码,例如

 < meta http-equiv =Content-Typecontent =text / html; charset = utf-8> 

对于您的数据库,您的表和mysql客户端设置是否为UTF-8设置?如果您使用mysql命令行客户端检查数据库,您的终端环境是否设置为期望UTF-8?



简而言之,您必须检查每一步:从原始源数据,触及它的代码,保留它的存储系统以及您使用的工具显示和调试它。


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.

这篇关于使用PHP处理GET参数中的unicode值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆