Home php教程 php手册 基于magic_quotes_gpc与magic_quotes_runtime的区别与使用介绍

基于magic_quotes_gpc与magic_quotes_runtime的区别与使用介绍

Jun 13, 2016 am 11:54 AM
magic quotes runtime and introduce use the difference based on data of

当你的数据中有一些   \  ”  ‘
这样的字符要写入到数据库里面,又想不被过滤掉的时候,它就很有用,会在这些字符前自动加上\,如
中国\地大物博”哈哈”
中国\\地大物博\”哈哈\”
可以使用set_maginc_quotes_runtime(0)关闭掉,当然你也可以直接在php.ini中设置。
get_magic_quotes_runtime() 取得 PHP 环境变量 magic_quotes_runtime 的值。

magic_quotes_gpc 为 on,它主要是对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。不要对已经被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义。遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。

两者不同

set_magic_quotes_runtime() 可以让程序员在代码中动态开启或关闭 magic_quotes_runtime,
set_magic_quotes_runtime(1) 表示开启,set_magic_quotes_runtime(0) 则表示关闭。当set_magic_quotes_runtime(1) 时,从数据库或通过fread之类的函数读取的文本,将自动对' “和\自动加上反斜杠\进行转义,防止溢出。这在对数据库的数据进行转移的时候非常有用。但在一般情况下,应当将其关闭,否则从数据库读取出来的数据单引号、双引号和反斜杠都会被加上\,导致显示不正常。像Discuz,PHPWind都在公共文件的头部加上一句 set_magic_quotes_runtime(0); 强制关闭 magic_quotes_runtime 。

magic_quotes_gpc

作用范围是:WEB客户服务端;
作用时间:请求开始是,例如当脚本运行时.

magic_quotes_runtime

作用范围:从文件中读取的数据或执行exec()的结果或是从SQL查询中得到的;
作用时间:每次当脚本访问运行状态中产生的数据.

所以

magic_quotes_gpc的设定值将会影响通过Get/Post/Cookies获得的数据,
magic_quotes_runtime的设定值将会影响从文件中读取的数据或从数据库查询得到的数据,
magic_quotes_gpc 是对通过GET、POST、COOKIE传递的数据进行转义,一般在数据入库前要先进行转义,
magic_quotes_gpc不能在代码中动态开启或关闭,需要到php.ini将magic_quotes_gpc设置为on或off,
代码中可以用get_magic_quotes_gpc获取magic_quotes_gpc的状态。
当magic_quotes_gpc为off时,需要手工对数据进行addslashes,代码如下:

复制代码 代码如下:


if (!get_magic_quotes_gpc()) { 
     new_addslashes($_GET); 
     new_addslashes($_POST); 
     new_addslashes($_COOKIE); 
 } 

 function new_addslashes($string) { 
     if (is_array($string)) { 
         foreach ($string as $key => $value) { 
             $string[$key] = new_addslashes($value); 
         } 
     } else { 
         $string = addslashes($string); 
     } 
     return $string; 
 }


另一示例:

复制代码 代码如下:


$data1 = $_POST['aaa']; 
 $data2 = implode(file('1.txt')); 

 if (get_magic_quotes_gpc()) { 
     //把数据$data1直接写入数据库 
 } else { 
     $data1 = addslashes($data1); 
     //把数据$data1写入数据库 
 } 

 if (get_magic_quotes_runtime()){ 
     //把数据$data2直接写入数据库 
     //从数据库读出的数据要经过一次stripslashes()之后输出 
 } else { 
     $data2 = addslashes($data2); 
     //把数据$data2写入数据库 
     //从数据库读出的数据直接输出 
 }

++++++++++++++++++++++++++++++++++++++++++++++++++++++

经验总结:

一、对于GPC,不管系统有没有开启magic_quotes_gpc(即php.ini中magic_quotes_gpc = On),我们统一开启 magic_quotes_gpc,对get、post、cookie的内容进行转义。操作如下:
(摘自uchome系统)

复制代码 代码如下:


function saddslashes($string) { 
     if (is_array($string)) { 
         foreach ($string as $key => $val) { 
             $string[$key] = saddslashes($val); 
         } 
     } else { 
         $string = addslashes($string); 
     } 
     return $string; 
 } 

 //GPC过滤 
 $magic_quote = get_magic_quotes_gpc(); 
 if(empty($magic_quote)) { 
     $_GET = saddslashes($_GET); 
     $_POST = saddslashes($_POST); 
 } 

 //COOKIE,给cookie值转义 
 $prelength = strlen($_SC['cookiepre']); 
 foreach ($_COOKIE as $key => $val) { 
     if(substr($key, 0, $prelength) == $_SC['cookiepre']) { 
         $_SCOOKIE[(substr($key, $prelength))] = empty($magic_quote) ? saddslashes($val) : $val; 
     } 
 }


二、对于magic_quotes_runtime,我们统一关闭它,即set_magic_quotes_runtime(0);不让从数据库读取出来的数据的单引号、双引号和反斜杠都自动被加上\。这样,对数据库的操作如下:添加数据到数据库之前,我们手动对数据进行addslashes(),而从数据库取出数据时,则作相反操作,即stripslashes()。

三、对于要序列化的内容,要保持裸数据,即要去掉转义,stripslashes(),然后在把序列化过的内容保存到数据库当中(注意,序列化过的内容是不带单引号(')、双引号(”)、反斜线(\)的),示例如下:
$feedarr['body_data'] = serialize(stripslashes($body_data));

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

出现Function set_magic_quotes_runtime() is deprecated 问题?

在安装PHPCMS出现Deprecated: Function set_magic_quotes_runtime() is deprecated 错误,查了一下网络及资料发现是PHP5.3和PHP6.0之后移除了set_magic_quotes_runtime()函数。
我可以使用如下方案替代:

view sourceprint?
 @set_magic_quotes_runtime(0);

view sourceprint?
 ini_set("magic_quotes_runtime", 0);

view sourceprint?
 if (phpversion()      set_magic_quotes_runtime(0); 
 }

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)

What are the basic requirements for c language functions What are the basic requirements for c language functions Apr 03, 2025 pm 10:06 PM

C language functions are the basis for code modularization and program building. They consist of declarations (function headers) and definitions (function bodies). C language uses values ​​to pass parameters by default, but external variables can also be modified using address pass. Functions can have or have no return value, and the return value type must be consistent with the declaration. Function naming should be clear and easy to understand, using camel or underscore nomenclature. Follow the single responsibility principle and keep the function simplicity to improve maintainability and readability.

How to set password protection for export PDF on PS How to set password protection for export PDF on PS Apr 06, 2025 pm 04:45 PM

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

Concept of c language function Concept of c language function Apr 03, 2025 pm 10:09 PM

C language functions are reusable code blocks. They receive input, perform operations, and return results, which modularly improves reusability and reduces complexity. The internal mechanism of the function includes parameter passing, function execution, and return values. The entire process involves optimization such as function inline. A good function is written following the principle of single responsibility, small number of parameters, naming specifications, and error handling. Pointers combined with functions can achieve more powerful functions, such as modifying external variable values. Function pointers pass functions as parameters or store addresses, and are used to implement dynamic calls to functions. Understanding function features and techniques is the key to writing efficient, maintainable, and easy to understand C programs.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Apr 05, 2025 pm 01:03 PM

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

What are the differences and connections between c and c#? What are the differences and connections between c and c#? Apr 03, 2025 pm 10:36 PM

Although C and C# have similarities, they are completely different: C is a process-oriented, manual memory management, and platform-dependent language used for system programming; C# is an object-oriented, garbage collection, and platform-independent language used for desktop, web application and game development.

How to use XPath to search from a specified DOM node in JavaScript? How to use XPath to search from a specified DOM node in JavaScript? Apr 04, 2025 pm 11:15 PM

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

What are the different ways of promoting H5 and mini programs? What are the different ways of promoting H5 and mini programs? Apr 06, 2025 am 11:03 AM

There are differences in the promotion methods of H5 and mini programs: platform dependence: H5 depends on the browser, and mini programs rely on specific platforms (such as WeChat). User experience: The H5 experience is poor, and the mini program provides a smooth experience similar to native applications. Communication method: H5 is spread through links, and mini programs are shared or searched through the platform. H5 promotion methods: social sharing, email marketing, QR code, SEO, paid advertising. Mini program promotion methods: platform promotion, social sharing, offline promotion, ASO, cooperation with other platforms.

See all articles