Home >Backend Development >PHP Tutorial >数据过滤、格式化用户输入 php札记

数据过滤、格式化用户输入 php札记

WBOY
WBOYOriginal
2016-06-13 11:01:501158browse

数据过滤、格式化用户输入 php笔记
1.过滤
abstract class Filter{
        protected $blackstr = array();
        protected $whitestr = array();
       
        abstract function filtit($str);
    }
    //过滤用户名的特殊符号
    class LoginFilter extends Filter {
        function filtit($str){
            $this->blackstr = array("/[\x7f-\xff]/","/\W/");
            return preg_replace($this->blackstr,"",$str);
        }
    }
    //对输入的文本框内容过滤
    class EditFilter extends Filter {
        function filtit($str){
            $this->blackstr = array("/\&/", "/\"/", "/\"/", "/\", "/\>/",
            "/\\\\/", "/\//", "/-/", "/\*/", "/ /" );
            $this->whitestr = array("&","'","","\'","/","-","*"," ");
            return preg_replace($this->blackstr,$this->whitestr,$str);
        }
    }



2.//用户的留言是一段代码:比如是js脚本;那么怎样及时避免数据的危害,取出数据时又正确显示

$js = "<script>alert('look me');</script>";//假设用户输入的是脚本
    //$str1 = base64_encode($js);//使用 MIME base64 对数据进行编码
    //echo $str1."
";
   
    //$str2 = base64_decode($str1);//
    echo htmlspecialchars($js);//转换特殊字符为HTML字符编码

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn