我是sql注入的受害者,他们入侵了我的mysql数据库,开始删除数据,并且更改了密码。我认为使用PDO是安全的。这是我的PHP登录页面。我需要帮助,我在哪里弄错了,导致了袭击。我想在如何改进此代码方面获得建议,以避免再次发生类似的攻击。
<?php session_start(); ?>
<?php
require_once('dbconnect/pdo.inc.php');
$username = (isset($_POST['username'])) ? trim($_POST['username']) : '';
$password = (isset($_POST['password'])) ? trim($_POST['password']) : '';
$pas = md5($password);
$redirect = (isset($_REQUEST['redirect'])) ? $_REQUEST['redirect'] :
'index.php';
$result = array();
$result['error'] = FALSE;
$result['message'] = "";
//if(isset($_POST['submit'])){
if (empty($password)) {
$result['error'] = true;
$result['message'] = "enter password";
//json encode and echo reusult.
$res = json_encode($result);
echo $res;
exit();
}
if (empty($username)) {
$result['error'] = true;
$result['message'] = "enter username";
//json encode and echo reusult.
$res = json_encode($result);
echo $res;
exit();
}
$query = ("SELECT username FROM users WHERE username=:username
AND password =:password");
$query_login = $con->prepare($query);
$query_login->execute(array(
':username' => $username,
':password' => $pas));
$results = $query_login->rowCount();
if ($results > 0){
$_SESSION['username'] = $username;
$_SESSION['logged'] = 1;
$result['error'] = false;
$result['message'] = 'Successfully logedin';
header('Location:index.php');
$res = json_encode($result);
echo $res;
}
else{
//set these explicitly just to make sure
$result['error'] = true;
$result['message'] = 'User name invalid';
header('Location:login.php');
$res = json_encode($result);
echo $res;
exit();
}
// }
?>
//这是我连接数据库的方式
<?php
function connected_Db(){
$dsn = 'mysql:host=localhost;dbname=usaDB;charset=utf8';
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
#echo "Yes we are connected";
return new PDO($dsn,'brrmingham','m#67yhfdelkomngf_()likf4', $opt);
}
$con = connected_Db();
if($con){
//echo "connected ";
}
else {
//echo "Connection faid ";
exit();
}
?>