如何在 PHP + MYSQL 中验证字符串



我正在使用带有准备语句的PDO。

我正在使用Javascript加密html文本区域中的文本,在PHP中解密,添加一些文本,并在将数据写入数据库之前重新加密数据。

我正在使用PHP从数据库解密数据并将其放入HTML5页面中。

通常,内容是 HTML 编码文本的结果。

添加斜杠、html实体和preg_replace...我能以最适合我的方式验证/过滤数据吗?

验证和筛选数据之间有什么区别?

我没有安全方面的经验。 请帮助我找到最适合我的申请的方法。

提前致谢

我认为这对我来说是一个很好的解决方案。

对此,你怎么看?

 function clearPasswrod($value){

     $value = trim($value); //remove empty spaces
     $value = strip_tags(); //remove html tags
     $value = htmlentities($value, ENT_QUOTES,'UTF-8'); //for major security transform some other chars into html corrispective...
      return $value;
 }
 function clearText($value){
     $value = trim($value); //remove empty spaces
     $value = strip_tags(); //remove html tags
     $value = filter_var($value, FILTER_SANITIZE_MAGIC_QUOTES); //addslashes();
     $value = filter_var($value, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); //remove /t/n/g/s
     $value = filter_var($value, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); //remove é à ò ì ` ecc...
     $value = htmlentities($value, ENT_QUOTES,'UTF-8'); //for major security transform some other chars into html corrispective...
     return $value;
 }
 function clearEmail($value){

     $value = trim($value); //remove empty spaces
     $value = strip_tags(); //remove html tags
     $value = filter_var($value, FILTER_SANITIZE_EMAIL); //e-mail filter;
     if($value = filter_var($value, FILTER_VALIDATE_EMAIL))
   {
     $value = htmlentities($value, ENT_QUOTES,'UTF-8');//for major security transform some other chars into html corrispective...
   }else{$value = "BAD";}  
     return $value;
 }

如果需要验证?试试这个。

function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }

就像$username = clean($_POST['username']);一样使用它

这些是在 PHP 5.3 中添加的,请阅读手册,看看这对你是否有帮助。

消毒过滤器:

http://www.php.net/manual/en/filter.filters.sanitize.php

验证过滤器:

http://www.php.net/manual/en/filter.filters.validate.php

最新更新