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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改