我以前使用 MySQL,被告知它不安全,所以现在我在 PDO 中重新编写了我的管理登录面板,这里的用户和其他论坛都说不能注入。但是黑客仍然在进入......我在登录后编辑了页面并告诉被黑客告诉我我在上面放了什么,黑客告诉我......
我需要知道我的代码是否安全。他告诉我他正在通过 SQL 进入。
因此,首先我将他们的 IP 存储在会话中,因此如果他们的 IP 更改,它将注销它们(或用户名)
if ( isset($_SESSION['last_ip']) == false )
{
$_SESSION['last_ip'] = $_SERVER['REMOTE_ADDR'];
}
if ( $_SESSION['last_ip'] !== $_SERVER['REMOTE_ADDR'] )
{
session_unset();
session_destroy();
}
然后这是我的登录信息:
session_start();
include 'functions/functions.php';
$db = mysqlconnect();
$password = md5($_POST['mypassword']);
$mod = 1;
$statement = $db->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
$statement->execute(array($_POST['myusername'],$password));
$result = $statement->fetchObject()->mod;
$count = $statement->rowCount();
if ( $result == 1 ) {
$db = mysqlconnect();
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['user'] = $_POST['myusername'] ;
//Test if it is a shared client
if ( !empty($_SERVER['HTTP_CLIENT_IP']) ) {
$ip=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
} elseif ( !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip=$_SERVER['REMOTE_ADDR'];
}
$sqll = "UPDATE users SET lastip=? WHERE username=?";
$q = $db->prepare($sqll);
$q->execute(array($ip,$_SESSION['username']));
$_SESSION['user'] = $_POST['myusername'] ;
$sqlll = "INSERT INTO user_log (username,ip) VALUES (?, ?)";
$qq = $db->prepare($sqlll);
$qq->execute(array($_SESSION['username'],$ip));
header("Location: home.php");
} else {
echo "Wrong Username or Password";
}
可以注入代码吗?
这是我的 home.php 页面,它阻止用户查看它。
/// My conenct is here
$sql = "SELECT * FROM users WHERE username='$_SESSION[user]'";
$result = mysql_query($sql) or die(mysql_error());
$values = mysql_fetch_array($result);
if( isset($_SESSION['user']) ) {
} else {
echo "Bye Bye";
die;
}
if ( $values['mod'] == 1 ) {
echo "welcome";
} else {
echo"Your account has been reported for hacking";
die;
}