Tuesday, 23 January 2018

Random password

Generates a random password by using given characters.

function random_password($length, $characters='abcdefgh1234567890'){

    if ($characters == ''){ return ''; }
    $chars_length = strlen($characters)-1;

    mt_srand((double)microtime()*1000000);

    $pwd = '';
    while(strlen($pwd) < $length){
        $rand_char = mt_rand(0, $chars_length);
        $pwd .= $characters[$rand_char];
    }

    return $pwd;

}

random_password(5,'abcd1234') => returns something like: '2b4d3'

0 comments:

Post a Comment