Friday 21 August 2015

How To Generate Password Using PHP

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;
}
?>

If you have any problem regarding this tutorial configuration please feel free to comment we love to answer your queries. 

1 comment: