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

// 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);

我需要打斜线吗?

最佳答案

使用准备好的语句更安全,这样(潜在的恶意)值与查询字符串分开,而不是依赖转义。了解有关PHP Data Objects的信息。

关于stripslashes(),仅当您打开PHP的magic_quotes_gpc功能时才需要这样做,而shouldn't因为不推荐使用而已启用。但是,如果您想变得更健壮,请执行if (get_magic_quotes_gpc()) $myusername = stripslashes($myusername);,以便在且仅当添加了斜线时才除去斜线层。

关于php - 避免在PHP中进行SQL注入(inject)的最安全方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5906318/

10-11 06:43