php搜索引擎中sql注入问题

来源:互联网 发布:三峡大学教务网络 编辑:程序博客网 时间:2024/06/06 00:58
请输入名字

用户名

1     admin

 

请输入名字

用户名

1     admin
2     guest

 

加入有100万条数据,就相当于攻击

 

请输入名字

用户名

 

 

<html>
<head>
<title>查询结果</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>

</head>
<?php

$keyWord=$_REQUEST['username'];
$conn=mysql_connect("localhost","root","root");
if(!$conn){
 die("不能连接到数据库".mysql_error());
}
mysql_select_db("spdb",$conn) or  die("选择数据库失败".mysql_error());
mysql_query("set  names  utf8",$conn) or  die("查询失败".mysql_error());
//如何防止查询sql查询
//对关键字进行过滤
$keyWord=addslashes($keyWord);
$keyWord=str_replace("%", "\%", $keyWord);
$keyWord=str_replace("_", "_%", $keyWord);

 

$sql="select  *  from users where username like '%$keyWord%'";

if(!empty($keyWord)){
 $res=mysql_query($sql,$conn) or  die("不能完成查询".mysql_error());
 while($row=mysql_fetch_array($res)){
  echo "<br/>$row[0]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$row[1]";
 }
}else{
 echo "请输入";
}

?>

 


1     admin
2     guest