I have the following function that checks for a user login. At it’s current state it checks either the username or email and the password ( hash ) and if the result match the ones in the db it returns some values ( please make abstraction of the other variables or functions that have no sense in there if you cannot see them ):
// Start Checking The Login Credentials
public function checkUserLogin($username, $password) {
$password = hash_hmac('sha512', $password, $this->salt($password));
if(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $username)){
$identifier = 'user_email';
} else {
$identifier = 'user_username';
}
$sql = 'SELECT user_username,user_level FROM users WHERE '.$identifier.' = ? AND user_password = ?';
// Check Login Attempts
if (isset($_SESSION['attempts']) && $_SESSION['attempts'] >= NUMBER_OF_ATTEMPTS) {
$lockdown = true;
$message['lockdown'] = true;
$message['message'] = SYSTEM_LOCKDOWN_MESSAGE;
return json_encode($message);
} else {
if ($stmt = $this->connect->prepare($sql)) {
$stmt->bind_param('ss', $username, $password);
$stmt->execute();
$stmt->bind_result($username, $level);
if ($stmt->fetch()) {
$stmt->close();
$_SESSION['member_logged_in'] = true;
$_SESSION['username'] = $username;
$_SESSION['level'] = $level;
$_SESSION['attempts'] = 0;
$ip = $this->getIP();
$sql = "UPDATE users SET user_last_login_date = NOW(), user_last_login_ip = '$ip' WHERE user_username = '$username'";
if ($stmt = $this->connect->prepare($sql)) {
$stmt->execute();
$stmt->close();
} else {
$error = true;
$message['error'] = true;
$message['message'] = CANNOT_PREPARE_DATABASE_CONNECTION_MESSAGE;
return json_encode($message);
}
$message['level'] = $level;
if( $level = 0 ) {
$_SESSION['standard'] = true;
} elseif( $level = 1 ) {
$_SESSION['special'] = true;
} elseif( $level = 2 ) {
$_SESSION['admin'] = true;
}
$error = false;
$message['error'] = false;
$message['message'] = SUCCESFUL_LOGIN_MESSAGE;
return json_encode($message);
} else {
@$_SESSION['attempts'] = $_SESSION['attempts'] + 1;
$error = true;
$message['error'] = true;
$message['message'] = FAILED_LOGIN_MESSAGE;
return json_encode($message);
}
}
}
}
Now, what I’m trying to do is before returning the values if the credentials are found in the db and match, check for another value in the db called user_disabled which can be either 0 or 1 and if the 1 value is found return another message, something like This account has been disabled and if 0 is found, continue with the rest of the code as it was before ( a successful login ).
I have the following code which does approximately what I need, but when I tried to place inside this public function it doesn’t work:
$sql = "SELECT user_disabled FROM users WHERE user_username = '$username'";
if ($stmt = $this->connect->prepare($sql)) {
$stmt->execute();
$stmt->bind_result($disabled);
$stmt->fetch();
$stmt->close();
if($disabled = 0){
/* Here is what should happen if the user is not blocked | The code after "$stmt->fetch()" */
} else {
@$_SESSION['attempts'] = $_SESSION['attempts'] + 1;
$error = true;
$message['error'] = true;
$message['message'] = 'ceva';
return json_encode($message);
}
} else {
$error = true;
$message['error'] = true;
$message['message'] = CANNOT_PREPARE_DATABASE_CONNECTION_MESSAGE;
return json_encode($message);
}
Could someone help me out with this because I cannot figure out how to do it right ?
The line
should read