+ -
当前位置:首页 → 问答吧 → 简单过滤post,get传递时安全问题

简单过滤post,get传递时安全问题

时间:2008-03-31

来源:互联网

这个类只是个简单的类,目前对xss攻击,和sql注入的预防也只是简单的过滤了下

欢迎大家对此类进行修改,或者提出你的建议.
复制PHP内容到剪贴板
PHP代码:

<?php
/**
* @name date safe class 0.1
* @author kevin xu
* @copyright kenvin E-mail:[email protected] MSN:[email protected]
*/
interface dateSafe{
 function gincn();
}
class safe extends doSafe implements dateSafe 
{
 public $safe;
 function __construct($safe)
 {
  parent::__construct($safe);       //调用父类构造函数,网友javachen找出来的错误
  $this->safe = $safe;
 }
 function gincn()
 {
  $this->safe = parent::xss($this->safe);
  $this->safe = parent::sql($this->safe);
  return $this->safe;
 }
}
class doSafe
{
 protected  $str;
 function __construct($str)
 {
  $this->str = $str;
 }
 function xss()
 { 
  $this->str = trim($this->str);         //清理空格字符
  $this->str = nl2br($this->str);         //将换行符转化为<br />
  $this->str = strip_tags($this->str);      //过滤文本中的HTML标签
  $this->str = htmlspecialchars($this->str);    //将文本中的内容转换为HTML实体
  $this->str = addslashes($this->str);      //加入字符转义
  return $this->str;
 }
 function sql()
 {
  $this->str = mysql_escape_string($this->str);
  return $this->str;
 }
}

?>

复制PHP内容到剪贴板
PHP代码:
//调用例子
<?php
$test = "ssss<html></html>";
$obj = new safe($test);
echo $obj->gincn();
?>

作者: gincn   发布时间: 2008-03-31

作者: luzhou   发布时间: 2008-03-31

作者: wuxp   发布时间: 2008-04-01

这个。。。算不算OO乱用。。。一个函数能搞定的事非要写个类

作者: yzxh24   发布时间: 2008-04-01

作者: luzhou   发布时间: 2008-04-01

我也觉得不必要

作者: mailangel123   发布时间: 2008-04-01

恩,应该考虑的

作者: sangern   发布时间: 2008-04-02

帮忙顶下

作者: zclmoon   发布时间: 2008-04-06

顶下

作者: lxydyx   发布时间: 2008-04-07

应该算

作者: maudo   发布时间: 2008-04-10

作者: gently   发布时间: 2008-04-10

功能是有了,

作者: readymydream   发布时间: 2008-04-10

我希望兄弟们能提点实质性的问题,比如还应该过滤哪些情况下的东西,或者把类扩展一下

作者: gincn   发布时间: 2008-04-16

OO没有错。
按了个接口,又实现一个继承个人感觉没有太大的必要。

[ 本帖最后由 micson 于 2008-4-16 16:47 编辑 ]

作者: micson   发布时间: 2008-04-16

<?php
$test = "ss'ss<html></html>";
$obj = new safe($test);
echo $obj->gincn();

?>
结果ss\\\'ss

作者: micson   发布时间: 2008-04-16

复制PHP内容到剪贴板
PHP代码:

<?php
/**
  * @desc Filter GPC variable
  * @param  String $allow_tags
  * @param  Array  $protect_words
  * @return  Array $gpc
  * */
function filterGpc($allow_tags='<b>', $protect_words='') {
  $gpc = array('get'=>$_GET, 'post'=>$_POST, 'cookie'=>$_COOKIE);
  foreach($gpc as $k1=>$v1) {
   foreach($v1 as $k2=>$v2) {
    $v2 = get_magic_quotes_gpc() ? $v2 : addslashes($v2);
    $v2 = trim($v2);
    $v2 = nl2br($v2);
        $v2 = strip_tags($v2, $allow_tags);
        $gpc[$k1][$k2] = $v2;
        if(! empty($protect_words)) {
         foreach($protect_words as $word) {
          if((strpos(strtolower($gpc[$k1][$k2]), $word) !== FALSE)) die("Hack!");
         }
        }
   }
  }
  unset($_GET, $_POST, $_COOKIE);
  return $gpc;
}
$allow_tags = "<b>";
$protect_words = array('insert', 'update', 'delete', 'select', 'from', 'or');
$gpc = filterGpc($allow_tags, $protect_words);
?>
刚刚写完的,之后使用$gpc变量例如 $gpc['post']['ff'] .
这是全局性的过滤.
之后每一个细节页面在使用其他的过滤规则,小站足够用了.
[ 本帖最后由 liuxingyuyuni 于 2008-4-21 14:19 编辑 ]

作者: liuxingyuyuni   发布时间: 2008-04-21