I'm trying to use the following script to test password strength:

<?
 		switch($_REQUEST['req']){
		case "change_password":
		
		 function 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"); 
		echo $strength;
		if($strength<3){
		echo 'Weak Password';
		}
		break;
		
		default:
		?>
<form method="post" action="password_test.php">
  <input name="password" type="password" id="password">
  <input type="hidden" name="req" value="change_password">
  <input type="submit" name="Submit" value="Submit">
</form>		
<?
				break;
		}
		?>

I'm having problem with getting the value of the $strength variable after the function passed. When I try to echo it or use it in the if statement in the usage part the value of $strength is empty. But if I change the "return $strength;" to "echo $strength;" Then I can see the integer value of $strength there. Anyone knows how I can get the value of $strength outiside he function or has any alternatives? Thanks

Recommended Answers

All 2 Replies

<?		
		//usage 
		$strength_value = CheckPasswordStrength("$password"); 
		echo $strength_value;

		if($strength_value<3){
		      echo 'Weak Password';
		}
		
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.