1
1
1programmer2018-04-21 14:26:36
PHP
1programmer, 2018-04-21 14:26:36

How to generate 5 random passwords in php?

Hi all.
There is a password generator:

function generate_password()
      {
        $arr = array('a','b','c','d','e','f',
                     'g','h','i','j','k','l',
                     'm','n','o','p','r','s',
                     't','u','v','x','y','z',
                     'A','B','C','D','E','F',
                     'G','H','I','J','K','L',
                     'M','N','O','P','R','S',
                     'T','U','V','X','Y','Z',
                     '1','2','3','4','5','6',
                     '7','8','9','0','.',',',
                     '(',')','[',']','!','?',
                     '&','^','%','@','*','$',
                     '<','>','/','|','+','-',
                     '{','}','`','~');
        // Генерируем пароль
        $pass = "";
           for($i = 0; $i < 10; $i++)
            {
                  // Вычисляем случайный индекс массива
                  $index = rand(0, count($arr) - 1);
                  $pass .= $arr[$index];
            }
        return $pass;
      }

which generates 1 random password. It is necessary that he generates, say, 10 passwords and writes them to an array. Tried like this
function generate_password()
      {
        $arr = array('a','b','c','d','e','f',
                     'g','h','i','j','k','l',
                     'm','n','o','p','r','s',
                     't','u','v','x','y','z',
                     'A','B','C','D','E','F',
                     'G','H','I','J','K','L',
                     'M','N','O','P','R','S',
                     'T','U','V','X','Y','Z',
                     '1','2','3','4','5','6',
                     '7','8','9','0','.',',',
                     '(',')','[',']','!','?',
                     '&','^','%','@','*','$',
                     '<','>','/','|','+','-',
                     '{','}','`','~');
        // Генерируем пароль
        $pass = "";
        $j = 0;
      	
           while ( $j <= 10) {
           	for($i = 0; $i < 10; $i++)
            { 
                  // Вычисляем случайный индекс массива
                  $index = rand(0, count($arr) - 1);
                  $pass .= $arr[$index];
                  $j++;
            }
            
           }
           return $result = array($pass);
    
        
      }

print_r(generate_password());

Where is the mistake ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2018-04-21
@demon416nds

and calling the function of generating one password from the cycle is not fate?

X
xmoonlight, 2018-04-21
@xmoonlight

$arr=[];
do $arr[]=generate_password(); while (count($arr)<10);
print_r($arr);

S
Sergey Sokolov, 2018-04-21
@sergiks

Another function, let it take the required number of passwords as a parameter and pull your original function so many times:

function generate_n_passwords($n) {
  $result = array();
  while($n--) {
    array_push( $result, generate_password());
  }
  return $result;
}

$myFivePasswords = generate_n_passwords(5);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question