基于magic_quotes_gpc与magic_quotes_runtime的区别与使用介绍_PHP
当你的数据中有一些 \ ” ‘
这样的字符要写入到数据库里面,又想不被过滤掉的时候,它就很有用,会在这些字符前自动加上\,如
中国\地大物博”哈哈”
中国\\地大物博\”哈哈\”
可以使用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);
}

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
