I was using MySQL before, and was told it was unsafe, so now I have recoded my admin login panel in PDO, which users here and other forums said can not be injected. But the hacker is still getting in... I edited the page after the login and told the hacked to tell me what I've put on it and the hacker told me...
I need to know if my code is safe. He is telling me that he is getting in though SQL.
So first I stored their IP in a session so if their IP changes it will log them out (or username)
if ( isset($_SESSION['last_ip']) == false )
{
$_SESSION['last_ip'] = $_SERVER['REMOTE_ADDR'];
}
if ( $_SESSION['last_ip'] !== $_SERVER['REMOTE_ADDR'] )
{
session_unset();
session_destroy();
}
Then here is my login:
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";
}
Can the code be injected ?
And this is my home.php page which stops users from viewing it.
/// 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;
}