This design is still under development!

Advanced Password Generator

Step 1:

First we will start of by creating a function, this function will take two parameters. The first ($lenght) is the amount of characters the password should have. The second is which character sets you want the password to consist of.


    function password($length, $character_sets = 'ulds'){

    }
        

Step 2:

We need to make sure that the length ($length) is an integer & be sure that the password will be atleast 12 characters long.


    if(!is_int($length)){
        return "Length of password have to be an integer";
    }
    
    if(!isset($length) || $length < 12){
        $length = 12; // Makes 12 the default length of the password if no number is inserted or length is under 12.
    }
        

Step 3:

The string sent in the characterset parameter gets split into an array called $charsets, which we can use later on to check what charactersets was selected.

Then we make an array with all the characters, each set on the corresponding position in the array (u = uppercase, l = lowercase etc.).


    $charsets = str_split($character_sets);
    
    $characters['u'] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $characters['l'] = 'abcdefghijklmnopqrstuvwxyz';
    $characters['d'] = '0123456789';
    $characters['s'] = '!#$%&*?@';
        

Step 4:

Now we move on to making the full string of characters, by checking the choosen $charset (u, l, d or s) - If they are selected, we put them in a new array, so we can filter the characters away, we don't need. We will use the new array later on.


    if(array_search('u', $charsets) !== FALSE)
        $selected[] = $characters['u'];
    if(array_search('l', $charsets) !== FALSE)
        $selected[] = $characters['l'];
    if(array_search('d', $charsets) !== FALSE)
        $selected[] = $characters['d'];
    if(array_search('s', $charsets) !== FALSE)
        $selected[] = $characters['s'];
        

Step 5:

This checks if any charactersets was selected and if none it stops the function and returns an error.


    if(count($selected) < 1)
        return "You have to select a character set by choosing between u, l, d or s. You can choose multiple character sets eg. ulds";
        

Step 6:

Now we can merge the selected characters into one string for generating the password.


    $use_characters = '';
    
    foreach($selected as $character){
        $use_characters .= $character;
    }
        

Step 7:

This is the last step in the process, here we take the string we just made with the selected character sets and shuffle it so the order is random. Then we cut it down to the desired length and shuffles the generated password once again just to add that extra randomness. Last we return the password so it can be either displayed or handled.


    return str_shuffle(substr(str_shuffle($use_characters), 0, $length));
        

Final result:


    function password($length, $character_sets = ''){
        
        if(!is_int($length)){
            return "Length of password have to be a integer";
        }
        
        if(!isset($length) || $length < 12){
            $length = 12; // Makes 12 the default length of the password if no number is inserted or length is under 12.
        }
        
        $charsets = str_split($character_sets);
        
        $characters['u'] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $characters['l'] = 'abcdefghijklmnopqrstuvwxyz';
        $characters['d'] = '0123456789';
        $characters['s'] = '!#$%&*?@';
    
        if(array_search('u', $charsets) !== FALSE)
            $selected[] = $characters['u'];
        if(array_search('l', $charsets) !== FALSE)
            $selected[] = $characters['l'];
        if(array_search('d', $charsets) !== FALSE)
            $selected[] = $characters['d'];
        if(array_search('s', $charsets) !== FALSE)
            $selected[] = $characters['s'];
    
        if(count($selected) < 1)
            return "You have to select a character set by choosing between u, l, d or s. You can choose multiple character sets eg. ulds";
        
        $use_characters = '';
        
        foreach($selected as $character){
            $use_characters .= $character;
        }
    
        return str_shuffle(substr(str_shuffle($use_characters), 0, $length));
        
    }
            

How to use:

To use the function simply write the code below.


    echo password(12, 'ulds');