dongshou1991 2014-04-25 12:14
浏览 35
已采纳

如何安全$ mysqli-> real_escape_string用于控制一个接受用户输入的选择查询

My PHP application has a query that takes a md5 hash as an input, from a user via GET method, then it applies $mysqli->real_escape_string() to it. After that it runs the SELECT statement.

How safe that function is? is it possible to SQL inject it or XSS it?

  • 写回答

4条回答 默认 最新

  • douhuireng4407 2014-04-25 12:18
    关注

    This is safe. If you dont feel safe, it only has characters and integers, you can easily test it is a md5 string (see example below). But again, there is no need for all of that.

    An alternative would be prepared statements. They're a bit more complex, but safe:

    $stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)");
    $stmt->bind_param("s", 'a1b2c3'); // s stands for String, i would be Integer
    $stmt->execute();
    

    This is a very simplefied example, the url above the codeblock explains more. Keep in mind that prepared statement have a overhead! doing this for 1 excecution per query will slow things down.


    Small example to check if a string could be a md5 hash:

    function isMd5($string){
        /// md5 strings are 32chars* long. Simple test, do that first:
        if( strlen($string)!==32){ return false; }
        // It only has chars (A-F) and integers, if any other character->not md5
        elseif( preg_match("^[0-9a-f]", $string) ){        return false; }
    
        // No errors, return true:
        return true;
    }
    // *rawmode ha 16 chars, but when you work with that, you'll know 
    

    This is not a very usefull function, because it will not really secure a lot, this is just to show you how you can verify info. You read the documentation to see the results, and make checks to test if it matches possible results.

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

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题