首页 > PHP > php开发安全程序

php开发安全程序

2009年03月29号
查看评论 发表评论 1,611次浏览

魔法引用magic_quotes_gpc
PHP提供magic_quotes_gpc魔法引用功能来保护我们的网站系统免受攻击,它会自动从用户的输入串中过滤特殊字符(’,”,\,\0 (NULL)),并减慢输入的过程。

检查使用magic_quotes_gpc的代码样例:

if (get_magic_quotes_gpc()) { //检查magic_quotes_gpc的打开状态

function strip_quotes(&$var) {

if (is_array($var){

array_walk($var, ‘strip_quotes’);

} else{

$var = stripslashes($var);

}

}//end func

// 处理GPC

foreach (array(‘GET’,’POST’,’COOKIE’) as $v){

if (!empty(${“_”.$v})){

array_walk(${“_”.$v}, ‘strip_quotes’);

}

}

// 处理上传时的文件名称

if (!empty($_FILES)){

foreach ($_FILES as $k => $v) {

$_FILES[$k]['name'] = stripslashes($v['name']);

}

}

}

PHP提供这个参数的初衷是好的,但是该功能从出现以来一直备受争议。实践表明,如果采用magic_quotes_gpc,则与不使用该函数相比,需要两倍多的内存来处理每条输入的元素,因此,如果非必要,我们可以在php.ini文件把该参数设置为关闭,不使用该功能,转用其他的方法为来处理。

其他高效的解决方案
由于magic_quotes_gpc的效率较低,我们使用其他方法来代替magic_quote_gpc魔法引用的功能,如下脚本:

if (get_magic_quotes_gpc()) {

$in = array(&$_GET, &$_POST, &$_COOKIE);

while (list($k,$v) = each($in)) {

foreach ($v as $key => $val) {

if (!is_array($val)) {

$in[$k][$key] = stripslashes($val);

continue;

}

$in[] =& $in[$k][$key];

}

}

unset($in);

}

页面: 1 2 3 4

类别PHP 标签
  1. mulberry sale
    发表于 2010年06月14号 14时12分56秒 | 1楼

    i hope so

  2. cheap timberland boots
    发表于 2010年06月16号 03时55分44秒 | 2楼

    what a nice post.what a great website :o ) thanks

  3. purple ghd iv styler
    发表于 2010年06月16号 07时43分07秒 | 3楼

    This is a fine example.

  4. Kooba Handbags
    发表于 2010年06月21号 13时15分03秒 | 4楼

    I guess I had to think more about what you wrote before i can comment on it..

  5. mulberry bag
    发表于 2010年06月22号 17时17分05秒 | 5楼

    Have a great day.
    LOADS OF SUNSHINE TO BE ON YOU TODAY.

  6. bikini swimwear
    发表于 2010年07月10号 16时17分04秒 | 6楼

    Whoop whoop. I have a feeling this is becoming the best.Keep it up!:)

  7. skechers kids
    发表于 2010年07月19号 17时50分59秒 | 7楼

    Even though it will not be here tomorrow, the effect of it will.

  8. lebron basketball shoes
    发表于 2010年07月19号 20时36分28秒 | 8楼

    Hallelujah!

  9. timberland 6 inch boots
    发表于 2010年07月24号 21时55分08秒 | 9楼

    These words mean a great deal to me today. Truly, it is necessary to let go – I see that.

  10. Louboutin sale
    发表于 2010年07月25号 12时11分16秒 | 10楼

    Well that’s a honest and true quotation

  11. discount puma shoes
    发表于 2010年08月02号 14时20分37秒 | 11楼

    It’s heroic.It is as the candle burns itself to give light to others.

  1. 目前没有通告
你必需 登陆 才能发表评论.