reenadak icon

random password generator

reenadak | PRO | 02/20/18 07:04:22 PM UTC | 0 ⭐ | 328 👁️ | Never ⏰ | []
PHP |

1.47 KB

|

None

|

0 👍

/

0 👎

    
    /*
    - Random Password Generator
    - Generates a password at random given specified limitations.
    
    $length               - The amount of characters the password should have.
    $useCapitalLetters    - Should the password consist of capital letters?
    $useNumbers           - Should the password consist of numbers?
    $useSpecialCharacters - Should the password consist of special characters?
    */
    
    public function randomPassword($length = 7, $useCapitalLetters = true, $useNumbers = true, $useSpecialCharacters = true) {
        $chars = array(
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
            'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
            'y', 'z');
        
        $specialChars = array(
            '!', '%', '^', '&', '*', '(', ')', '@');
                
        $pass = '';
        
        for ($i = 0; $i < $length; $i++) {
            $rand = rand(0, 3);
            
            switch ($rand) {
            case 0:
                $pass .= $chars[rand(0, count($chars) - 1)];
                break;
            case 1:
                if ($useCapitalLetters)
                    $pass .= strtoupper($chars[rand(0, count($chars) - 1)]);
                else
                    $pass .= $chars[rand(0, count($chars) - 1)];
                
                break;
            case 2:
                if ($useNumbers)
                    $pass .= rand(0, 9);
                else
                    $pass .= $chars[rand(0, count($chars) - 1)];
                
                break;
            case 3:
                if ($useSpecialCharacters)
                    $pass .= $specialChars[rand(0, count($specialChars) - 1)];
                else
                    $pass .= $chars[rand(0, count($chars) - 1)];
                
                break;
            }
        }
        
        return $pass;
    }

Comments