Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Password strength check in PHP [closed]

I am trying to create a password check script. I already have checks for email (for not allowed characters) like this:

  public function checkEmail($email)
  {
    if (filter_var($email, FILTER_VALIDATE_EMAIL))
      return true;
    else
      return false;   
  }

So I am looking for a password validation function that checks passwords have at least one alphanumeric character, and one numeric character, and a minimum of 8 characters, and also provides error messages.

like image 788
Byakugan Avatar asked May 25 '12 10:05

Byakugan


People also ask

How can I compare password and confirm password in PHP?

Just get both the password and confirm password fields in the form submit PHP and test for equality: if ($_POST["password"] === $_POST["confirm_password"]) { // success! } else { // failed :( } where password and confirm_password are the IDs of the HTML text inputs for the passwords.

What is password strength checker?

Password strength is a measure of the effectiveness of a password against guessing or brute-force attacks. In its usual form, it estimates how many trials an attacker who does not have direct access to the password would need, on average, to guess it correctly.

How do I validate my username and password in HTML?

var text1 = document. getElementById("username"); This says "get the element by its id: username". Please check the HTML section of the example.


1 Answers

public function checkPassword($pwd, &$errors) {
    $errors_init = $errors;

    if (strlen($pwd) < 8) {
        $errors[] = "Password too short!";
    }

    if (!preg_match("#[0-9]+#", $pwd)) {
        $errors[] = "Password must include at least one number!";
    }

    if (!preg_match("#[a-zA-Z]+#", $pwd)) {
        $errors[] = "Password must include at least one letter!";
    }     

    return ($errors == $errors_init);
}

Edited version of this: http://www.cafewebmaster.com/check-password-strength-safety-php-and-regex

like image 142
Jeroen Avatar answered Oct 03 '22 08:10

Jeroen