How To Generate a Random Password in PHP

August 30, 2010

Here’s a quick little snippet to generate a random password in php:

function createRandomPassword($length = 7) {
    $chars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789!@#$%^&*(){}[]";

    $pass = '' ;

    while(strlen($pass) <= $length) 
    {
        $pass .= $chars[mt_rand(0, strlen($chars))];
    }
    return $pass;
}

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive