password strength checker

Started on the old forum in May 2012 · view the original in the archive · old links redirect here

6 replies · 2,132 views · started 14 years ago · 1 min read

4 participants
GumSlone Administrator Principal · 1,690 posts #1

after searching web for very simple, nice working php password checker i couldn't find any good solution for my needs, so i decided to code my own password strength checker, some function parts were taken from functions which ive found but they were not good enough for me.

here is a function:

function check_password_strength($password) 
{
	$score = array("Blank","Very Weak","Weak","Medium","Strong","Very Strong");
	$strength = password_strength_score($password);

	return $score[$strength-1];
}
function password_strength_score($password) 
{
	$strength = 0; 
	
	$length = strlen($password);
	if ($length < 1)
		return 1;
	elseif ($length < 4)
		return 2;
	else $strength = 1;
	
	$clean_string = preg_replace('/[^[:alnum:]]/i', '', $password);
	
	if(strlen($clean_string) != $length) $strength++;
	elseif($length < 5) return 2;
	
	if(($length - strlen($clean_string))>1)
		$strength++;
	
	if ($length > 7)
		$strength++;

	
	$patterns = array("#[a-z]#","#[A-Z]#","#[0-9]#");
	foreach($patterns AS $pattern) 
	{
		if(preg_match($pattern,$password)) 
		{ 
			$strength++; 
		} 
	}
	if($strength>6)$strength=6;
	
	return $strength;
}

if someone has better functions please share here.

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Loony Senior · 258 posts #2

iPhone post

Checks the strength of a password and return an integer value depening on it's strength

CheckPasswordStrength($password) 
{ 

$strength = 0; 
$patterns = array('#[a-z]#','#[A-Z]#','#[0-9]#','/[¬!"£$%^&*()`{}\[\]:@~;\'#<>?,.\/\\-=_+\|]/'); 
foreach($patterns as $pattern) 
{ 
if(preg_match($pattern,$password,$matches)) 
{ 
$strength++; 
} 
} 
return $strength; 

// 1 - weak 
// 2 - not weak 
// 3 - acceptable 
// 4 - strong 
} 

//usage 
CheckPasswordStrength('password'); 

Creator of Epix.Mobi

Keep an Eye on us Big things coming soon!!!! Need something for your site hit me up here http://coding-talk.com/forum/main-forums/marketplace/153110-free-features-for-your-wapsite

GumSlone Administrator Principal · 1,690 posts #3

Loony wrote: Checks the strength of a password and return an integer value depening on it's strength

CheckPasswordStrength($password) 
{ 

$strength = 0; 
$patterns = array('#[a-z]#','#[A-Z]#','#[0-9]#','/[¬!"£$%^&*()`{}\[\]:@~;\'#<>?,.\/\\-=_+\|]/'); 
foreach($patterns as $pattern) 
{ 
if(preg_match($pattern,$password,$matches)) 
{ 
$strength++; 
} 
} 
return $strength; 

// 1 - weak 
// 2 - not weak 
// 3 - acceptable 
// 4 - strong 
} 

//usage 
CheckPasswordStrength('password'); 

yes i saw this function, actually i took a small part from it.

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Anshul Senior · 296 posts #4

after searching web for very simple, nice working php password checker i couldn't find any good solution for my needs, so i decided to code my own password strength checker, some function parts were taken from functions which ive found but they were not good enough for me.

here is a function:

$patterns = array('#[a-z]#','#[A-Z]#','#[0-9]#','/[¬!"£$%^&*()`{}\[\]:@~;\'#<>?,.\/\\-=_+\|]/'); 
		
GumSlone Administrator Principal · 1,690 posts #6

Anshul wrote:

$patterns = array('#[a-z]#','#[A-Z]#','#[0-9]#','/[¬!"£$%^&*()`{}\[\]:@~;\'#<>?,.\/\\-=_+\|]/'); 
		

i have removed the last pattern and added some own code which checks if there are non alphanumeric symbols in the string, my code would work with symbols/chars like: üöансвкмцßв and not only with '/[¬!"£$%^&*()`{}[]:@~;'#<>?,./\-=_+|]/'

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

metulj Super moderator Principal · 1,726 posts #7
 <?php
function checkPasswordStrength($password, $username = false)
{
$returns = array(
'strength' => 0,
'error'    => 0,
'text'     => '');

$length = strlen($password);
	if ($length < 8)
	{
	$returns['error']    = 1;
	$returns['text']     = 'The password is not long enough';
	}
		else
		{
		//check for a couple of bad passwords:
		if ($username && strtolower($password) == strtolower($username))
		{
		$returns['error']    = 4;
		$returns['text']     = 'Password cannot be the same as your Username';
		}
			elseif (strtolower($password) == 'password')
			{
			$returns['error']    = 3;
			$returns['text']     = 'Password is too common';
			}
				else
				{
				preg_match_all ("/(.)\1{2}/", $password, $matches);
				$consecutives = count($matches[0]);
				preg_match_all ("/\d/i", $password, $matches);
				$numbers = count($matches[0]);
				preg_match_all ("/[A-Z]/", $password, $matches);
				$uppers = count($matches[0]);
				preg_match_all ("/[^A-z0-9]/", $password, $matches);
				$others = count($matches[0]);
						//see if there are 3 consecutive chars (or more) and fail!
						if ($consecutives > 0)
							{
							$returns['error']    = 2;
							$returns['text']     = 'Too many consecutive characters';
							}
								elseif ($others > 1 || ($uppers > 1 && $numbers > 1))
								{
								//bulletproof
								$returns['strength'] = 5;
								$returns['text']     = 'Virtually Bulletproof';
								}
								elseif (($uppers > 0 && $numbers > 0) || $length > 14)
								{
								//very strong
								$returns['strength'] = 4;
								$returns['text']     = 'Very Strong';
								}
							else if ($uppers > 0 || $numbers > 2 || $length > 9)
							{
							//strong
							$returns['strength'] = 3;
							$returns['text']     = 'Strong';
							}
							else if ($numbers > 1)
						{
						//fair
						$returns['strength'] = 2;
						$returns['text']     = 'Fair';
						} 			
					else 		
					{
					//weak
					$returns['strength'] = 1;
					$returns['text']     = 'Weak';
					}
				}
		}
return $returns;
}
?>

i've found this one, it doesn't look bad

It's better to keep your mouth shut and give the impression that you're stupid, than to open it and remove all doubt. ⓣⓗⓔ ⓠⓤⓘⓔⓣⓔⓡ ⓨⓞⓤ ⓑⓔ©ⓞⓜⓔ, ⓣⓗⓔ ⓜⓞⓡⓔ ⓨⓞⓤ ⓐⓡⓔ ⓐⓑⓛⓔ ⓣⓞ ⓗⓔⓐⓡ ! ιη тнєσяу, тнє ρяα¢тι¢є ιѕ α яєѕυℓт σƒ тнє тнєσяу, вυт ιη ρяα¢тι¢є ιѕ тнє σρρσѕιтє.

キノgんイノ刀g 4 ア乇ムc乇 ノ丂 レノズ乇 キucズノ刀g 4 √ノ尺gノ刀ノイリ!

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close