set_magic_quotes_runtime()和get_magic_quotes_gpc()的用法举例
1、PHP中set_magic_quotes_runtime()函数的作用:
修改PHP.ini文件中的 magic_quotes_runtime 变量的状态,如果想获得magic_quotes_runtime 变量的状态用get_magic_quotes_runtime这个函数如果返回0表示本功能被关闭,如果返回1表示本功能已经开启。
magic_quotes_runtime的功能
当它被开启时所有外部引入的 数据库资料 或 文件 等都会自动转为含有反斜线溢出字符的资料。
比如:
用户向数据库提交的数据中含有\" '这些符号时它就会在这些符号的前面自动加上"\"转义符。
PHP4之前,此属性默认关闭,php4以后默认开启,可以用set_magic_quotes_runtime(0)将其关闭。
2、get_magic_quotes_gpc函数作用:
此函数取得 PHP 环境配置的变量 magic_quotes_gpc (GPC, Get/Post/Cookie) 值。返回 0 表示关闭本功能;返回 1 表示本功能打开。当
magic_quotes_gpc 打开时,所有的 ' (单引号), " (双引号), \ (反斜线) and 空字符会自动加上转义符\;
默认情况下,PHP 指令 magic_quotes_gpc 为 on,它主要是对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。不要对已经被 magic_quotes_gpc转义过的字符串使用 addslashes(),因为这样会导致双层转义。遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。
多用于判断有PHP有没有自动调用addslashes 这个函数,
来看具体 的例子吧。
<?php
echo get_magic_quotes_gpc(); // 检测,输出0
echo
tiny_mce_marker
POST['name']; // jason'name
echo addslashes(
tiny_mce_marker
POST['name']); // jason\'name
if (!get_magic_quotes_gpc()) {
$name = addslashes(
tiny_mce_marker
POST['name']);
} else {
$name =
tiny_mce_marker
POST['name'];
}
echo $name; // jason\'name
//安全写入到数据库了
?>
以下例子中,把两个函数都做了处理。
if(version_compare(PHP_VERSION,'6.0.0','<') ) {
@set_magic_quotes_runtime (0);
define('MAGIC_QUOTES_GPC',get_magic_quotes_gpc()?True:False);
}
另外,还可以用ini_get和ini_set读取和设置系统配置:
您可能感兴趣的文章:
set_magic_quotes_runtime()和get_magic_quotes_gpc()的用法举例
php中get_magic_quotes_gpc用法介绍
PHP中错误处理的一些方法
php字符转义函数参考
php函数get_magic_quotes_gpc
php中什么是魔术引号
php addslashes 递归实现反斜线引用字符串的方法
php ini_set函数无效怎么解决
Deprecated: Function set_magic_quotes_runtime()
PHP常用转义字符函数