954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Passsword Strength Checker

Passsword Strength Checker

<?php


function testPassword($password)
{
    if ( strlen( $password ) == 0 )
    {
        return 1;
    }

    $strength = 0;

    /*** get the length of the password ***/
    $length = strlen($password);

    /*** check if password is not all lower case ***/
    if(strtolower($password) != $password)
    {
        $strength += 1;
    }
    
    /*** check if password is not all upper case ***/
    if(strtoupper($password) == $password)
    {
        $strength += 1;
    }

    /*** check string length is 8 -15 chars ***/
    if($length >= 8 && $length <= 15)
    {
        $strength += 1;
    }

    /*** check if lenth is 16 - 35 chars ***/
    if($length >= 16 && $length <=35)
    {
        $strength += 2;
    }

    /*** check if length greater than 35 chars ***/
    if($length > 35)
    {
        $strength += 3;
    }
    
    /*** get the numbers in the password ***/
    preg_match_all('/[0-9]/', $password, $numbers);
    $strength += count($numbers[0]);

    /*** check for special chars ***/
    preg_match_all('/[|!@#$%&*\/=?,;.:\-_+~^\\\]/', $password, $specialchars);
    $strength += sizeof($specialchars[0]);

    /*** get the number of unique chars ***/
    $chars = str_split($password);
    $num_unique_chars = sizeof( array_unique($chars) );
    $strength += $num_unique_chars * 2;

    /*** strength is a number 1-10; ***/
    $strength = $strength > 99 ? 99 : $strength;
    $strength = floor($strength / 10 + 1);

    return $strength;
}

/*** example usage ***/
$password = 'qwer1234';
echo testPassword($password);

?>
ayesha789
Posting Pro in Training
496 posts since Jun 2009
Reputation Points: 17
Solved Threads: 7
 

Hey, will this give you a password strength instantly?, because I thought php would be run on the server side. If that's the case, is it possible to have a client_side code maybe in 'javascript' that gives instant password strength?
Thanks for your input anyway!

webish
Newbie Poster
9 posts since Jun 2009
Reputation Points: 13
Solved Threads: 0
 

No, Its gives strengths from the server. if u have javascript code please share with all.

Ayesha

ayesha789
Posting Pro in Training
496 posts since Jun 2009
Reputation Points: 17
Solved Threads: 7
 

Here is an interesting page that could be used to create a password strength meter:

http://www.lockdown.co.uk/?pg=combi

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

Thanks Digital-ether

ayesha789
Posting Pro in Training
496 posts since Jun 2009
Reputation Points: 17
Solved Threads: 7
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You