Home php教程 php手册 ThinkPHP入库出现两次反斜线转义及数据库类转义的解决方法

ThinkPHP入库出现两次反斜线转义及数据库类转义的解决方法

Jun 06, 2016 pm 08:18 PM
thinkphp twice escape

这篇文章主要介绍了ThinkPHP入库出现两次反斜线转义及数据库类转义的解决方法,主要通过针对magic_quotes_gpc开启的情况下进行检查与判断转义来实现,需要的朋友可

本文实例讲述了ThinkPHP入库出现两次反斜线转义及数据库类转义的解决方法。分享给大家供大家参考。具体方法如下:

这种情况是在 magic_quotes_gpc 开启的情况下发生的。原因是thinkphp在入库的时候没有判断 magic_quotes_gpc 是否开启,不管三七二十一地进行了转义处理。
解决办法是在入口文件增加如下代码则可:

复制代码 代码如下:

if (!get_magic_quotes_gpc()) { 
    function addslashes_deep($value) {  
        $value = is_array($value) ? 
            array_map('addslashes_deep', $value) : 
            addslashes($value); 
        return $value; 
   } 
   $_POST = array_map('addslashes_deep', $_POST); 
   $_GET = array_map('addslashes_deep', $_GET); 
   $_COOKIE = array_map('addslashes_deep', $_COOKIE); 
   $_REQUEST = array_map('addslashes_deep', $_REQUEST); 
}

有人这样修改DbMysql.class.php中的转义函数:

复制代码 代码如下:

public function escape_string($str) { 
if (get_magic_quotes_gpc()) { 
return $str; 

if($this->_linkID) { 
return mysql_real_escape_string($str,$this->_linkID);
}else{ 
return mysql_escape_string($str); 

}

 

其实这种方法并不可取!因为如果魔术函数on时,而$str又不是post或get得来(比如读取文本、数据库),它还是没加反斜线。
所以我不管$str是否已经被转义,,一律先去除转义,然后再加上转义。这样就避免了二次转义,也避免了遗漏转义。
下面是我的修改方法:

复制代码 代码如下:

public function escape_string($str) { 
$str = stripslashes($str); 
if($this->_linkID) { 
return mysql_real_escape_string($str,$this->_linkID); 
}else{ 
return mysql_escape_string($str); 

}

希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

Why does event bubbling trigger twice? Why does event bubbling trigger twice? Feb 22, 2024 am 09:06 AM

Why does event bubbling trigger twice? Event bubbling (Event Bubbling) means that in the DOM, when an element triggers an event (such as a click event), the event will bubble up from the element to the parent element until it bubbles to the top-level document object. . Event bubbling is part of the DOM event model, which allows developers to bind event listeners to parent elements, so that when child elements trigger events, the events can be captured and processed through the bubbling mechanism. However, sometimes developers encounter events that bubble up and trigger twice.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

See all articles