dtpxi88884 2016-02-02 07:42
浏览 14

mysqli_real_escape_string vs filter_input()? 我应该使用什么方法?

I don't know when to use each one of them.

$name = mysqli_real_escape_string($connection, $_POST['name']); 

or

$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
  • 写回答

1条回答 默认 最新

  • dongzeao5047 2016-02-02 07:55
    关注

    real_escape_string() have to be used for the sql strings, i.e. parts of the query enclosed in quotes. Have to be used unconditionally, despite of whatever previous manipulations. real_escape_string() Escapes special characters in a string for use in an SQL statement, taking into account the current char set of the connection.

    Where as

    filter_input Gets a specific external variable by name and optionally filters it. filter_input will provide you way to validate input for specific string and characters.

    1. Validate filters
    2. Sanitize filters
    3. Other filters
    4. Filter flags

    Validate filters

    As name suggested it is use for validation for specific input like FILTER_VALIDATE_EMAIL.

    $email = "abc@example"; // wrong email
    
    if(filter_var($email, FILTER_VALIDATE_EMAIL)){
        echo $email.'<br>';
        var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
    }else{
        var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));   
    } 
    

    Sanitize filters it will use for validate and remove characters from string.

    FILTER_SANITIZE_EMAIL   "email"         Remove all characters except letters, digits and !#$%&'*+-=?^_`{|}~@.[]. 
    

    For more information on filter_value.

    So I think that both have different roles to play.

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探