-3

我在下面的代码中的列表行上得到一个未定义的偏移量。任何想法为什么?即使我收到偏移错误,仍然会生成密码

function generatePassword($length=6,$level=2){

   list($usec, $sec) = explode(' ', microtime(true));
   srand((float) $sec + ((float) $usec * 100000));  

   $validchars[1] = "23456789abcdefghjkmnpqrstuvwxyz";
   $validchars[2] = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
   $validchars[3] = "23456789_!@#$%&*()-= /abcdefghjkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ_!@#$%&*()-= /";

   $password  = "";
   $counter   = 0;

   while ($counter < $length) {
     $actChar = substr($validchars[$level], rand(0, strlen($validchars[$level])-1), 1);

     // All character must be different
     if (!strstr($password, $actChar)) {
        $password .= $actChar;
        $counter++   ;
   }
   }

   re

转$密码;

}

4

1 回答 1

1

microtime(true)返回一个从不包含空格的浮点值。因此,explode()返回一个只有一个项目和list两个参数的数组会失败,因为这需要数组中至少有两个项目。改用microtime(false)

list($usec, $sec) = explode(' ', microtime(false));
于 2013-02-02T15:00:12.960 回答