When we are building any application from where authentication required, then we need to generate strong password. This article will explain how we can generate password using PHP. Here we have just created one function “generateStrongPassword” having 2 parameters one is length and second is strength. Using these parameters we can customize the generated password.
<?php
function generateStrongPassword($length=9, $strength=4) {
$vowels = 'aeuy';
$string = 'bcdfghjmnpqrstvz';
if ($strength & 1) {
$string .= 'BCDFGHJLMNPQRSTVWXZ';
}
if ($strength & 2) {
$vowels .= "AEUY";
}
if ($strength & 4) {
$string .= '23456789';
}
if ($strength & 8) {
$string .= '@#$!';
}
$password = '';
$alt = time() % 2;
for ($i = 0; $i < $length; $i++) {
if ($alt == 1) {
$password .= $string[(rand() % strlen($string))];
$alt = 0;
} else {
$password .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
}
return $password;
}
$vowels = 'aeuy';
$string = 'bcdfghjmnpqrstvz';
if ($strength & 1) {
$string .= 'BCDFGHJLMNPQRSTVWXZ';
}
if ($strength & 2) {
$vowels .= "AEUY";
}
if ($strength & 4) {
$string .= '23456789';
}
if ($strength & 8) {
$string .= '@#$!';
}
$password = '';
$alt = time() % 2;
for ($i = 0; $i < $length; $i++) {
if ($alt == 1) {
$password .= $string[(rand() % strlen($string))];
$alt = 0;
} else {
$password .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
}
return $password;
}
?>
Thank you dear for such a wonderful tutorial.
ReplyDelete