在php的配置文件中,有个布尔值的设置,就是magic_quotes_runtime,当它打开时,
php的大部分函数自动的给从外部引入的(包括数据库或者文件)数据中的溢出字符加上反
斜线。 固然若是重复给溢出字符加反斜线,那么字符串中就会有多个反斜线,因此这时
就要用set_magic_quotes_runtime()与get_magic_quotes_runtime()设置和检测php.ini
文件中magic_quotes_runtime状态。 为了使本身的程序无论服务器是什么设置都能正常
执行。能够在程序开始用get_magic_quotes_runtime检测该设置的状态决定是否要手工处理
,或者在开始(或不须要自动转义的时候)用set_magic_quotes_runtime(0)关掉该设置。
magic_quotes_gpc设置是否自动为GPC(get,post,cookie)传来的数据中的\'\"\\加上反斜
线。能够用get_magic_quotes_gpc()检测系统设置。若是没有打开这项设置,能够使用
addslashes()函数添加,它的功能就是给数据库查询语句等的须要在某些字符前加上了反
斜线。这些字符是单引号(\')、双引号(\")、反斜线(\\)与 NUL(NULL 字符)。
通常用法以下;
if(!get_magic_quotes_gpc())
{
addslashes($prot);
}
其实这个函数就是判断有PHP有没有自动调用addslashes 这个函数,
下面是例子,其实也是从手册上弄下来的,传过来就为本身看着方便,由于本身记性很差..
见笑:
<
html
>
<!--以POST方式传过去一个带有单引号的字符串 -->
<
body
>
<
form
action
="first.php"
method
="post"
>
<
input
type
="text"
name
="lastname"
value
="Simao'pig"
>
<
input
type
="submit"
value
="提交"
>
</form>
</body>
</html>
<?php
echo get_magic_quotes_gpc(); // 很很差意思,个人这个是0
echo $_POST['lastname']; //
Simao'pig
echo addslashes($_POST['lastname']); //
Simao\'pig
if (!get_magic_quotes_gpc()) {
$lastname = addslashes($_POST['lastname']);
} else {
$lastname = $_POST['lastname'];
}
echo $lastname; //
Simao\'pig
$sql = "INSERT INTO lastnames (lastname) VALUES ('$lastname')";
?>