解决ThinkPHP中以Get模式提交中文字符乱码
解决ThinkPHP中以Get方式提交中文字符乱码
?
接触thinkPHP还没几天,都还没入门呢,因为公司项目非常着急,自己也对thinkPHP很有兴趣想借机会提高一下,就着急的用thinkPHP开始了,当然是遇到了非常多的问题,不过还好通过朋友gidot的帮助及网络还有官方的手册,基本都解决了。
这几天又遇到个问题,就是GET提交中文会乱码的问题。在论坛看了下好像很多人都有而且也没个好的解决办法。
数据库、页面编码、文件保编码这些全都是UTF8是100%确定没有问题的。
有的人提供的说可行的方法,到我这里也不行,可能是因为我环境是WIDNOWS2003+IIS6.0的默认字符集为中文有关系。试遍了各种方法始终不行。
后来看说客户端base64编码了再发送请求,然后服务器上再BASE64解码。试了下,发现PHP的base64结果居然和JS的不一样-_-|||,后来想,问题是出在传递时的编码上,用base64也只是把字符转换成字母以避免被再次编码解码出现问题。
既然如此,那自己写个简单的编解码应该也能解决,试了下,果然解决了,现在分享一下我的方法。
先给客户端发送出的数据,编码一下,免得传递时被浏览器编码了到服务器出问题,我的解决办法是,发送表单时,先用JS给字符encodeURI编码了,然后将里面的百分号“%”替换成别的不会被编码的字符(注意这个字符要比较特别,不要和用户输入的字符混淆,也不能过长,考虑到一个字符容易和用户输入混淆,我选了3个字符“.s.”,),再让表单发送。具体实现:
id="group_name"?title="组名查询" class="medium" >
然后在服务器端写个函数,把我们编码的数据解码成正常的:
//解码GET传值
function getChineseGetValue($key){
if (strlen($key)>0){
??return?urldecode(str_replace('.s.','%',$key));
}
}
然后我们接收参数查询时要解码:
最后在页面显示GET值的时候,也要解码:
}" type="text" name="group_name" id="group_name" title="组名查询" class="medium" >
这样就可以了,希望本帖对有此问题的童鞋能有所帮助,
附带说明下,那个 getId 是我的一个JS函数,偷懒用的(可以不用那么麻烦的老写document.getElementById……),代码如下:
return document.getElementById(id);
}
?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
