Clive Walkden

PHP, MySQL, HTML5, CSS3, jQuery code and ideas.

PHP Posts

PHP function to generate a random password

PHP function to generate a random password

in PHP
by
with 0 Comments

This simple function generates a random password using all alphanumeric characters. While this won't be the most secure password, it is better than a dictionary word. The function accepts an integer to determine the password length, but if one isn't passed defaults to 8.

function password_generator($length = 8)
{
    $str = 'abcdefghijkmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    for ($i = 0, $password = ''; $i < $length; $i++) {
        $password .= substr($str, mt_rand(0, strlen($str) - 1), 1);
    }
    return $password;
}
comments powered by Disqus