drq231358 2019-06-19 23:02
浏览 85

可以使用此代码来验证$ _GET变量吗?

I am building a page where GET parameters are used and i am wondering if this piece of code successfully evaluates the next conditions: $_GET["id"] must be an integrer and $_GET["status"] must be a "true" or "false".

$rawId = $_GET["id"];
$rawStatus = $_GET["status"];

$Id = filter_var($rawId, FILTER_SANITIZE_NUMBER_INT);
$Id = filter_var($Id, FILTER_VALIDATE_INT);

if (!$Id) {
    die();
}

if ($rawStatus != "true" && $rawStatus != "false") {
    die();
}
  • 写回答

3条回答 默认 最新

  • doom910730 2019-06-19 23:13
    关注

    You can use is_int and is_bool to achieve this. Make sure you are also checking if the $_GET vars are set before you do this to avoid potential notices

    $rawId = (isset($_GET["id"]) ? $_GET["id"] : null);
    $rawStatus = (isset($_GET["status"]) ? $_GET["status"] : null);
    
    if (!is_int($rawId)) {
         //handle
    }
    
    if (!is_bool($rawStatus)) {
         //handle
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 SQL server表计算问题
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出