当前位置:Gxlcms > 数据库问题 > 预防数据库攻击的正确做法

预防数据库攻击的正确做法

时间:2021-07-01 10:21:17 帮助过:26人阅读

function check_input($value) { // 去除斜杠 if (get_magic_quotes_gpc()) { $value = stripslashes($value); } // 如果不是数字则加引号 if (!is_numeric($value)) { $value = "‘" . mysql_real_escape_string($value) . "‘"; } return $value; } $con = mysql_connect("localhost", "hello", "321"); if (!$con) { die(‘Could not connect: ‘ . mysql_error()); } // 进行安全的 SQL $user = check_input($_POST[‘user‘]); $pwd = check_input($_POST[‘pwd‘]); $sql = "SELECT * FROM users WHERE user=$user AND password=$pwd"; mysql_query($sql); mysql_close($con); ?>

get_magic_qoutes_gpc():

当magic_quotes_gpc=On的时候,函数get_magic_quotes_gpc()就会返回1

当magic_quotes_gpc=Off的时候,函数get_magic_quotes_gpc()就会返回0

magic_quotes_gpc函数在php中的作用是判断解析用户提示的数据,如包括有:post、get、cookie过来的数据增加转义字符“\”,以确保这些数据不会引句起程序,特别是数据库语因为特殊字符引起的污染而出现致命的错误。

stripslashes():剔除get_magic_qoutes_gpc()转义。

mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符。

下列字符受影响:

  • \x00
  • \n
  • \r
  • \
  • "
  • \x1a

预防数据库攻击的正确做法

标签:

人气教程排行