D S Kaletha

Web Designer & Developer

Generate a Automatic Password in php

how to generate random passwords that are highly secure and extremely difficult to crack. However you can choose between various complexity/strength and you can set password length as well.Simple method or Traditional way  to generate password is using rand() function: concatenate several random-selected letters together. Something like this:

<?php
function generate_password($len = 6){
    $r = '';
    for($i=0; $i<$len; $i++)
        $r .= chr(rand(0, 25) + ord('a'));
    return $r;
}?>
Combination of rand() and md5() function is often used as a simpler alternative
<?phpfunction gen_md5_password($len = 6){    // function calculates 32-digit hexadecimal md5 hash    // of some random data
 return substr(md5(rand().rand()), 0, $len);
}?>

Time is important factor to generate a distinct value.The function accept two parameters, $length, the desired length of the password, and $strength, the desired strength of 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;
}

Tags: ,

Leave a Reply