CodeIgniter 完美解决URL含有中文字符串,codeigniterurl
CodeIgniter 完美解决URL含有中文字符串,codeigniterurl
codeIgniter默认的配置下是不允许URL中包含非ASCII字符的,如果URL中含非ASCII字符,那么CI会毫不客气的抛出错误。本文章向码农介绍CodeIgniter 如何解决URL含有中文字符串。
你 可能会说,那我把这个URL使用函数urlencode一下呢?不行。因为Web Server会在接收到一个被urlencode的URL后自动将其decode,然后在PHP里得到的这些字符串转换成他原来所代表的含义,并使用 Web Server自己的URL编码字符集(IIS6 中文版是GBK,Apache 2.2是UTF-8)传送给应用程序,这就使得CI得到的URL已经是一个解码过的,无论你有没有对URL进行urlencode,浏览器在发出请求时会 自动检测,若没有,则会自动进行编码。所以,手动的进行urlencode并不能解决问题。那么我们应该怎么做来解决这个问题呢?
对于CI这种框架,用到现在,我的观点是尽量不要去修改它,而是去扩展他,CI提供了很好的扩展机制,我们只需要在application/core/下(2.0版本以前的为application/libraries/)增加一个文件MY_URI.php,其内容为:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_URI extends CI_URI { /** * 自定义的url过滤函数 * * @access private * @param string * @return string */ function _filter_uri($str) { if ($str != '' AND $this->config->item('permitted_uri_chars') != '') { $str = urlencode($str); if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str)) { exit('The URI you submitted has disallowed characters.'); } $str = urldecode($str); } return $str; } }
我覆盖了原来CI_URI中的_filter_uri方法,这样就可以使得中文的URL通过检测。但是,如果URL里有空格, 也不行了,怎么办呢?原来,urlencode会将空格转换成+,而CI的默认配置中是不允许+出现在URL里的,OK,把
$config ['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
改成
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\+\-';
就可以了。
或者
第一步把config.php 中的
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
替换成
$config['permitted_uri_chars'] ='a-z 0-9~%.:_-u4e00-u9fa5';
面所有的操作我们都做完了,但是接下来你可能又遇到了新的问题,那就是url中得到的中文信息是乱码,不知道在你的服务器上会不会遇到该问题,但是我遇到了(IIS)。但是在本地是正常的,本地用的是apache。
好,我在程序中,把$_SERVER['REQUEST_URI']打印出来,发现它就是乱码的,苦思冥想,这是怎么回事?这就说明在我得到uri中的参数之前,就已经被编码了,好,我们使用iconv解码:
iconv("gb2312","UTF-8",$uri);
现在打印出来看看,好了,原来的中文参数打印出来了,是正确的。
这个问题解决了,但是还是有一个疑问,url为什么是gb2312编码的呢,如果在我的程序中使用(我采用的是utf-8编码),还需要转换为utf-8编码,是不是和web服务器是有关系的呢,希望大家能够帮忙解答。
以上这篇CodeIgniter 完美解决URL含有中文字符串就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持帮客之家。

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

Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

Excel spreadsheet is one of the office software that many people are using now. Some users, because their computer is Win11 system, so the English interface is displayed. They want to switch to the Chinese interface, but they don’t know how to operate it. To solve this problem, this issue The editor is here to answer the questions for all users. Let’s take a look at the content shared in today’s software tutorial. Tutorial for switching Excel to Chinese: 1. Enter the software and click the "File" option on the left side of the toolbar at the top of the page. 2. Select "options" from the options given below. 3. After entering the new interface, click the “language” option on the left

"WWE2K24" is a racing sports game created by Visual Concepts and was officially released on March 9, 2024. This game has been highly praised, and many players are eagerly interested in whether it will have a Chinese version. Unfortunately, so far, "WWE2K24" has not yet launched a Chinese language version. Will wwe2k24 be in Chinese? Answer: Chinese is not currently supported. The standard version of WWE2K24 in the Steam Chinese region is priced at 199 yuan, the deluxe version is 329 yuan, and the commemorative edition is 395 yuan. The game has relatively high configuration requirements, and there are certain standards in terms of processor, graphics card, or running memory. Official recommended configuration and minimum configuration introduction:

Tips for solving Chinese garbled characters written by PHP into txt files. With the rapid development of the Internet, PHP, as a widely used programming language, is used by more and more developers. In PHP development, it is often necessary to read and write text files, including txt files that write Chinese content. However, due to encoding format problems, sometimes the written Chinese will appear garbled. This article will introduce some techniques to solve the problem of Chinese garbled characters written into txt files by PHP, and provide specific code examples. Problem analysis in PHP, text

How to change Chinese to English in Google Chrome? Some friends want to set Google Chrome to English so that they can continuously improve their English during use. So how to set it to English? Google Chrome is Chinese by default. Below, I will show you how to set the language of Google Chrome to English. Let’s take a look. Setting steps: 1. Open [Google Chrome], as shown in the figure below. 2. Click the [three dots] menu in the upper right corner of the Google Chrome interface, as shown in the figure below. 3. After entering the menu page, find [Settings], as shown in the figure below. 4. After entering the settings page, click the [Language] option, as shown in the figure below. 5. Select [Add Language] in the language interface, as shown in the figure below.

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,
