» Snippets » php » Automatic password creation

Automatic password creation

  • It is a very common fact that user choose or set their password by own. But sometimes your application need to create random password for user. Like we can suggest strong password for user on their request. The following function is very easy to use and you can choose the desired length and strength for the password.

    function generatePassword($length=9, $strength=0) {
    	$vowels = 'aeuy';
    	$consonants = 'bdghjmnpqrstvz';
    	if ($strength >= 1) {
    		$consonants .= 'BDGHJLMNPQRSTVWXZ';
    	}
    	if ($strength >= 2) {
    		$vowels .= "AEUY";
    	}
    	if ($strength >= 4) {
    		$consonants .= '23456789';
    	}
    	if ($strength >= 8 ) {
    		$vowels .= '@#$%';
    	}
    
    	$password = '';
    	$alt = time() % 2;
    	for ($i = 0; $i < $length; $i++) {
    		if ($alt == 1) {
    			$password .= $consonants[(rand() % strlen($consonants))];
    			$alt = 0;
    		} else {
    			$password .= $vowels[(rand() % strlen($vowels))];
    			$alt = 1;
    		}
    	}
    	return $password;
    }

    Enjoy!

Publish your snippets with us & be an exclusive author!

  • Total View: 421 time(s)
  • Script Upload Date: 2012-07-01

Recommended Scripts More Recommended Snippets: