How to manually check if a username and password is correct? There are a lot of answers on how to log in as a specific user. But I can't find any answer on how to check if the username and password is valid without logging in. I want to be able to do something like this:

$username = "bla@bla.com"
$password = "blablabla"

$valid = checkIfValidDetails($username, $password);


I simply want $valid = true if the username and password is correct, and false if not. 

How can I do that?
Drupal 6 and 7

user_authenticate($username, $password);


Drupal 8

// Inject user.auth service..
$uid = $this-&gt;userAuth-&gt;authenticate($username, $password);
// or
$uid = \Drupal::service('user.auth')-&gt;authenticate($username, $password);


Source: How to check logged in user username/password/role using Drupal function for validation