php 字符過(guò)濾類(lèi),用于過(guò)濾各類(lèi)用戶(hù)輸入的數(shù)據(jù)
更新時(shí)間:2009年05月27日 01:22:50 作者:
最近老看到有人的網(wǎng)站被掛馬,發(fā)一個(gè)php的字符過(guò)濾類(lèi),建議廣大站長(zhǎng)朋友們多關(guān)注下,安全方面的知識(shí)。
詳細(xì)代碼如下:
<?php
abstract class Filter { //filter parent class
private $blackstr = array();
private $whitestr = array();
function filtit($str) {
//do something
}
}
class LoginFilter extends Filter { //for user login filte username(過(guò)濾注冊(cè)的用戶(hù)名)
function filtit($str) {
$this -> blackstr = array(
´/[\x7f-\xff]/´, //filter chinese include chinese symbol
´/\W/´ //filter all english symbol
);
return preg_replace($this->blackstr, ´´, $str);
}
}
class EditorFilter extends Filter { //for article editor filter(過(guò)濾在線(xiàn)編輯器內(nèi)容)
function filtit($str) {
$this -> blackstr = array(
´/\&/´,
´/\´/´,
´/\"/´,
´/\</´,
´/\>/´,
´/\\\\/´,
´/\//´,
´/-/´,
´/\*/´,
´/ /´
);
$this -> whitestr = array(
´&´,
´'´,
´"´,
´<´,
´>´,
´\´,
´/´,
´-´,
´*´,
´ ´
);
return preg_replace($this->blackstr, $this -> whitestr, $str);
}
}
class SQLFilter extends Filter { //for filte sql query string(過(guò)濾如查詢(xún)或其它sql語(yǔ)句)
function filtit($str) {
$this -> blackstr = array(
´/\´/´,
´/-/´
);
return preg_replace($this->blackstr, ´´, $str);
}
}
class FileNameFilter extends Filter { //for filte a file name(過(guò)濾文件名如下載文件名)
function filtit($str) {
$this -> blackstr = array(
´/[^A-za-z0-9_\.]|\\\\|\^|\[|\]/´
);
return preg_replace($this->blackstr, ´´, $str);
}
}
?>
使用方法如:
$filter = new FileNameFilter(); //定義實(shí)例
$downFile = $filter->filtit($_GET[´fn´]); //調(diào)用過(guò)濾方法
復(fù)制代碼 代碼如下:
<?php
abstract class Filter { //filter parent class
private $blackstr = array();
private $whitestr = array();
function filtit($str) {
//do something
}
}
class LoginFilter extends Filter { //for user login filte username(過(guò)濾注冊(cè)的用戶(hù)名)
function filtit($str) {
$this -> blackstr = array(
´/[\x7f-\xff]/´, //filter chinese include chinese symbol
´/\W/´ //filter all english symbol
);
return preg_replace($this->blackstr, ´´, $str);
}
}
class EditorFilter extends Filter { //for article editor filter(過(guò)濾在線(xiàn)編輯器內(nèi)容)
function filtit($str) {
$this -> blackstr = array(
´/\&/´,
´/\´/´,
´/\"/´,
´/\</´,
´/\>/´,
´/\\\\/´,
´/\//´,
´/-/´,
´/\*/´,
´/ /´
);
$this -> whitestr = array(
´&´,
´'´,
´"´,
´<´,
´>´,
´\´,
´/´,
´-´,
´*´,
´ ´
);
return preg_replace($this->blackstr, $this -> whitestr, $str);
}
}
class SQLFilter extends Filter { //for filte sql query string(過(guò)濾如查詢(xún)或其它sql語(yǔ)句)
function filtit($str) {
$this -> blackstr = array(
´/\´/´,
´/-/´
);
return preg_replace($this->blackstr, ´´, $str);
}
}
class FileNameFilter extends Filter { //for filte a file name(過(guò)濾文件名如下載文件名)
function filtit($str) {
$this -> blackstr = array(
´/[^A-za-z0-9_\.]|\\\\|\^|\[|\]/´
);
return preg_replace($this->blackstr, ´´, $str);
}
}
?>
使用方法如:
復(fù)制代碼 代碼如下:
$filter = new FileNameFilter(); //定義實(shí)例
$downFile = $filter->filtit($_GET[´fn´]); //調(diào)用過(guò)濾方法
您可能感興趣的文章:
- php過(guò)濾HTML標(biāo)簽、屬性等正則表達(dá)式匯總
- php正則過(guò)濾html標(biāo)簽、空格、換行符的代碼(附說(shuō)明)
- PHP過(guò)濾★等特殊符號(hào)的正則
- PHP中過(guò)濾常用標(biāo)簽的正則表達(dá)式
- PHP正則表達(dá)式過(guò)濾html標(biāo)簽屬性(DEMO)
- php中過(guò)濾非法字符的具體實(shí)現(xiàn)
- PHP中字符安全過(guò)濾函數(shù)使用小結(jié)
- php表單敏感字符過(guò)濾類(lèi)
- php過(guò)濾所有惡意字符(批量過(guò)濾post,get敏感數(shù)據(jù))
- PHP使用正則表達(dá)式實(shí)現(xiàn)過(guò)濾非法字符串功能示例
相關(guān)文章
php實(shí)現(xiàn)向javascript傳遞數(shù)組的方法
這篇文章主要介紹了php實(shí)現(xiàn)向javascript傳遞數(shù)組的方法,涉及php數(shù)組轉(zhuǎn)json傳遞到j(luò)avascript的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07PHP curl CURLOPT_RETURNTRANSFER參數(shù)的作用使用實(shí)例
這篇文章主要介紹了PHP curl CURLOPT_RETURNTRANSFER參數(shù)的作用使用實(shí)例,CURLOPT_RETURNTRANSFER參數(shù)的作用是把CRUL獲取的內(nèi)容賦值到變量,需要的朋友可以參考下2015-02-02php上傳文件并存儲(chǔ)到mysql數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了php上傳文件并存儲(chǔ)到mysql數(shù)據(jù)庫(kù)的方法,以完整實(shí)例形式較為詳細(xì)的分析了php操作文件上傳與數(shù)據(jù)庫(kù)存儲(chǔ)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03php中http_build_query 的一個(gè)問(wèn)題
http_build_query 遠(yuǎn)程攻擊者可以利用漏洞獲得敏感內(nèi)存信息。請(qǐng)大家謹(jǐn)慎使用2012-03-03用mysql觸發(fā)器自動(dòng)更新memcache的實(shí)現(xiàn)代碼
不錯(cuò)的一篇文章,用于項(xiàng)目中可以帶來(lái)更多的便利,按照方法已經(jīng)調(diào)試成功,可以大大提高項(xiàng)目的速度。2009-10-10