php安全处理 过滤函数,php 安全过滤函数代码

php 安全过滤函数代码

发布于 2015-01-21 12:58:46 | 149 次阅读 | 评论: 0 | 来源: 网友投递

PHP开源脚本语言PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于Web开发领域。PHP的文件后缀名为php。

本文是一个php 实现的安全过滤函数代码,防止用户恶意输入内容,感兴趣的同学参考下。

//安全过滤输入[jb]

function check_str($string, $isurl = false)

{

$string = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F]/','',$string);

$string = str_replace(array("�","%00","r"),'',$string);

empty($isurl) && $string = preg_replace("/&(?!(#[0-9]+|[a-z]+);)/si",'&',$string);

$string = str_replace(array("%3C",'

$string = str_replace(array("%3E",'>'),'>',$string);

$string = str_replace(array('"',"'","t",' '),array('"',''',' ',' '),$string);

return trim($string);

}

下面是为大家整理的一些过滤函数:

/**

* 安全过滤类-过滤javascript,css,iframes,object等不安全参数 过滤级别高

* Controller中使用方法:$this->controller->fliter_script($value)

* @param string $value 需要过滤的值

* @return string

*/

function fliter_script($value) {

$value = preg_replace("/(javascript:)?on(click|load|key|mouse|error|abort|move|unload|change|dblclick|move|reset|resize|submit)/i","&111n\2",$value);

$value = preg_replace("/(.*?)/si","",$value);

$value = preg_replace("/(.*?)/si","",$value);

$value = preg_replace ("//iesU", '', $value);

return $value;

}

/**

* 安全过滤类-过滤HTML标签

* Controller中使用方法:$this->controller->fliter_html($value)

* @param string $value 需要过滤的值

* @return string

*/

function fliter_html($value) {

if (function_exists('htmlspecialchars')) return htmlspecialchars($value);

return str_replace(array("&", '"', "'", ""), array("&", """, "'", ""), $value);

}

/**

* 安全过滤类-对进入的数据加下划线 防止SQL注入

* Controller中使用方法:$this->controller->fliter_sql($value)

* @param string $value 需要过滤的值

* @return string

*/

function fliter_sql($value) {

$sql = array("select", 'insert', "update", "delete", "'", "/*",

"../", "./", "union", "into", "load_file", "outfile");

$sql_re = array("","","","","","","","","","","","");

return str_replace($sql, $sql_re, $value);

}

/**

* 安全过滤类-通用数据过滤

* Controller中使用方法:$this->controller->fliter_escape($value)

* @param string $value 需要过滤的变量

* @return string|array

*/

function fliter_escape($value) {

if (is_array($value)) {

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

$value[$k] = self::fliter_str($v);

}

} else {

$value = self::fliter_str($value);

}

return $value;

}

/**

* 安全过滤类-字符串过滤 过滤特殊有危害字符

* Controller中使用方法:$this->controller->fliter_str($value)

* @param string $value 需要过滤的值

* @return string

*/

function fliter_str($value) {

$badstr = array("�", "%00", "r", '&', ' ', '"', "'", "", " ", "%3C", "%3E");

$newstr = array('', '', '', '&', ' ', '"', ''', "", " ", "");

$value = str_replace($badstr, $newstr, $value);

$value = preg_replace('/&((#(d{3,5}|x[a-fA-F0-9]{4}));)/', '&\1', $value);

return $value;

}

/**

* 私有路劲安全转化

* Controller中使用方法:$this->controller->filter_dir($fileName)

* @param string $fileName

* @return string

*/

function filter_dir($fileName) {

$tmpname = strtolower($fileName);

$temp = array(':/',"�", "..");

if (str_replace($temp, '', $tmpname) !== $tmpname) {

return false;

}

return $fileName;

}

/**

* 过滤目录

* Controller中使用方法:$this->controller->filter_path($path)

* @param string $path

* @return array

*/

public function filter_path($path) {

$path = str_replace(array("'",'#','=','`','$','%','&',';'), '', $path);

return rtrim(preg_replace('/(/){2,}|(\){1,}/', '/', $path), '/');

}

/**

* 过滤PHP标签

* Controller中使用方法:$this->controller->filter_phptag($string)

* @param string $string

* @return string

*/

public function filter_phptag($string) {

return str_replace(array(''), array('', '?>'), $string);

}

/**

* 安全过滤类-返回函数

* Controller中使用方法:$this->controller->str_out($value)

* @param string $value 需要过滤的值

* @return string

*/

public function str_out($value) {

$badstr = array("", "%3C", "%3E");

$newstr = array("", "");

$value = str_replace($newstr, $badstr, $value);

return stripslashes($value); //下划线

}

相关阅读:

php 安全过滤函数代码

PHP防注入漏洞过滤函数代码

php下过滤HTML代码的函数

php常用的安全过滤函数集锦

PHP中字符安全过滤函数应用小结

PHP验证码函数代码示例

具有时效性的php加密解密函数代码

PHP字符过滤函数去除字符串最后一个逗号(rtrim)

PHP输出时间差函数代码示例

PHP分页函数代码

PHP 在线翻译函数代码

php xml分析函数代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值