Friday, 17 June 2016

Simple PHP Password Generator

When building web based applications, often you need to generate passwords for users. This is a simple PHP script which allows you to generate random passwords.
<?php
/**
 * Generates random passwords.
 * @param   int  Password Length (Default : 10)
 * @return  Random String
 * @author  Harshal Limaye (http://coffeecupweb.com/)
 */
function genPass($len = 10) {
    $charPool = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array();
    $length = strlen($charPool) - 1;
    for ($i = 0; $i < $len; $i++) {
        $n = rand(0, $length);
        $pass[] = $charPool[$n];
    }
    return implode($pass);
}
echo genPass();
?>

0 comments:

Post a Comment