您的位置:首页 > 其它

set_magic_quotes_runtime()和get_magic_quotes_runtime()

2009-05-25 16:12 645 查看
set_magic_quotes_runtime()和get_magic_quotes_runtime() 是针对数据库

get_magic_quotes_gpc() 是针对GPC, Get/Post/Cookie

来自http://www.5iphp.com/zh-hans/content/325.html

来自http://hi.baidu.com/samyucn/blog/item/df14cb3590a03c46251f14da.html

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.0以后的版本如果程序要用到将它关闭的时候直接写成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 这个函数,

下面是例子

echo get_magic_quotes_gpc(); // 很不好意思,我的这个是0

echo POST['name']; // jason'name

echo addslashes(POST['name']); // jason\'name

if (!get_magic_quotes_gpc()) {

$name = addslashes(POST['name']);

} else {

$name =POST['name'];

}

echo $name; // jason\'name

//这样输入的数据可以安全的写入到数据库了。

?>

set_magic_quotes_runtime()和get_magic_quotes_runtime()

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" >

<?php

$host="localhost";

$name="root";

$password="123";

$conn=mysql_connect($host,$name,$password) or die (mysql_error());

mysql_select_db("test",$conn) or die (mysql_error());

mysql_query("SET NAMES 'GBK'");

/*在数据库中实际内容为(我是"ZENGFENG" $a=$b+$c)*/

/*=============================================*/

echo get_magic_quotes_runtime()."<br>";//0

$sql="SELECT * FROM testtable WHERE id=1";

$query=mysql_query($sql) or die (mysql_error());

$rows=mysql_fetch_array($query,MYSQL_ASSOC);

echo $rows[con]."<br><br>";//输出(我是"ZENGFENG" $a=$b+$c)

/*=============================================*/

/*---------------------------------------------*/

set_magic_quotes_runtime(1);

//ini_set(magic_quotes_runtime,1);//这行和上行效果一样

echo get_magic_quotes_runtime()."<br>";//1

$sql="SELECT * FROM testtable WHERE id=1";

$query=mysql_query($sql) or die (mysql_error());

$rows=mysql_fetch_array($query,MYSQL_ASSOC);

echo $rows[con]."<br>";//输出(我是\"ZENGFENG\" $a=$b+$c)

/*---------------------------------------------*/

?>

//输出

0

我是"ZENGFENG" $a=$b+$c

1

我是\"ZENGFENG\" $a=$b+$c


get_magic_quotes_gpc()

<?php

echo get_magic_quotes_gpc()."<br>";//默认返回1

echo $_POST[user]."<br>";//我在文件框内打(ZF"eng"$b)返回(ZF\"eng\"$b)

ini_set(magic_quotes_gpc,0);  //无效,设置不了。看来他不像是magic_quotes_runtime()

get_magic_quotes_gpc()."<br>";//返回1

echo $_POST[user]."<br>";//我在文件框内打(ZF"eng"$b)返回(ZF\"eng\"$b)

if (!get_magic_quotes_gpc()) {

$lastuser = addslashes($_POST['user']);

} else {

$lastuser = $_POST['user'];

}

echo $lastuser."<br>";//返回(ZF\"eng\"$b)

?>

//输出

1

ZF\"eng\"$b

1

ZF\"eng\"$b

ZF\"eng\"$b

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

因些我们看出magic_quotes_gpc()只能手动到服务器配置文件中改

我在 PHP设置->关闭后magic_quotes_gpc 如下

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<?php

echo get_magic_quotes_gpc()."<br>";//返回0

echo $_POST[user]."<br>";//我在文件框内打(ZF"eng"$b)返回(ZF"eng"$b)

if (!get_magic_quotes_gpc()) {

$lastuser = addslashes($_POST['user']);

} else {

$lastuser = $_POST['user'];

}

echo $lastuser."<br>";//返回(ZF\"eng\"$b)

?>

//输出

0

ZF"eng"$b

ZF\"eng\"$b
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: