duanmeng2842 2010-02-25 20:10
浏览 78
已采纳

在PHP中输入过滤?

Is this link sufficent for example for input filtering form data? With a post for example?

<?php
$var=300;

$int_options = array(
"options"=>array
  (
  "min_range"=>0,
  "max_range"=>256
  )
);

if(!filter_var($var, FILTER_VALIDATE_INT, $int_options))
  {
  echo("Integer is not valid");
  }
else
  {
  echo("Integer is valid");
  }
?> 

What is the most common kind of filtering? Like sanitizing strings and numbers. I use preg_match for validation of email fields on the server side and regular expression checks in javascript. I'm no validation nazi but would like to have some sort of filtering for the most common things.

These kind of things I think I could abstract away in my application with some public static functions in a class for example, like this

  Validate::String($str);
     Validate::Interger($int);

What do you think about that?

  • 写回答

3条回答 默认 最新

  • dsxsou8465 2010-02-25 20:19
    关注

    filter_var() is a good start. If you are planning on using these inputs in any type of SQL statement, you should look into properly sanitizing it for that, too.

    PDO with prepared statements, mysql_real_escape_string or any other db wrapper (MBD2, etc...) should provide this functionality for you.

    I guess the key idea here is that there is a difference between filtering and sanitizing data, and there are different levels of doing each. It's very much a multi-part process.

    For filtering, you could do a type check (is this an int?) and then validate that the input meets your criteria (is this int between 1 and 128?)

    You'll also need to sanitize the data. htmlspecialchars for output, some proper quoting and escaping for use in SQL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等