【问题标题】:The safest way to avoid SQL injection in PHP?在 PHP 中避免 SQL 注入的最安全方法是什么?
【发布时间】:2011-05-06 02:58:45
【问题描述】:

我只是想知道这行代码是否可以安全使用以避免 SQL 注入?

// username and password sent from form 
$myusername=$_POST['loginUserName']; 
$mypassword=$_POST['loginPassword'];

$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

我需要使用stripslashes吗?

【问题讨论】:

    标签: php security sql-injection


    【解决方案1】:

    使用准备好的语句更安全,这样(潜在的恶意)值与查询字符串分离,而不是依赖转义。阅读PHP Data Objects

    关于stripslashes(),只有在您启用了PHP 的magic_quotes_gpc 功能时才需要这样做,而shouldn't 因为它已被弃用。但是,如果您想变得健壮,请执行if (get_magic_quotes_gpc()) $myusername = stripslashes($myusername);,以便当且仅当添加斜线时,它才会删除一层斜线。

    【讨论】:

      猜你喜欢
      • 2014-12-15
      • 2020-05-05
      • 2015-06-22
      • 2010-09-07
      • 2017-11-10
      • 2013-06-10
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      相关资源
      最近更新 更多