paolo@bimodesign.com | +34 608 61 64 10

Framework

        

Php - Random String Password

I'm using this simple PHP functions in order to create a random password which has this properties
- Parametric the string length (default 8 char)
- Use lowercase letters (0/1 default 1)
- Use uppercase letters (0/1 default 1)
- Use special characters (0/1 default 1)
- Use numbers (0/1 default 1)
- Use prefix (default blank string)

private function randomGeneratePassword($length = 8, 
                                            $uselower = 1, 
                                            $useupper = 1, 
                                            $usespecial = 1, 
                                            $usenumbers = 1,
                                            $prefix = '',
    ) {

        $key = $prefix;
        // Seed random number generator
        srand((double) microtime() * rand(1000000, 9999999));
        $charset = "";
        if ($uselower == 1)
            $charset .= "abcdefghijkmnopqrstuvwxyz";
        if ($useupper == 1)
            $charset .= "ABCDEFGHIJKLMNPQRSTUVWXYZ";
        if ($usenumbers == 1)
            $charset .= "0123456789";
        if ($usespecial == 1)
            $charset .= "_+-=][";
       
        while ($length > 0) {
            $key .= $charset[rand(0, strlen($charset) - 1)];
            $length--;
        }
       
        return $key;
    }

This algoritm will use typically to generate ONLY the string. So you must use a MD5(), SHA1() or much better crypt() function in order to create a hashed string.