PHP中通过转义字符串防止SQL注入攻击

来源:互联网 发布:淘宝上oem化妆品能买不 编辑:程序博客网 时间:2024/04/30 16:03

 PHP 的内置 mysql_real_escape_string() 函数用作任何用户输入的包装器。这个函数对字符串中的字符进行转义,使字符串不可能传递撇号等特殊字符并让 MySQL 根据特殊字符进行操作。

$sql = "select count(*) as ctr  from users where 
  username='foo' and password='' or '1'='1' limit 1";
  
$sql = "select count(*) as ctr from users where 
username='".mysql_real_escape_string($username)."' 
and password='". mysql_real_escape_string($pw)."' limit 1"; 


select count(*) as ctr from users where \
username='foo' and password='\' or \'1\'=\'1' limit 1"

原创粉丝点击