How to generate random password with PHP?

后端 未结 19 2105
栀梦
栀梦 2020-12-01 00:45

Or is there a software to auto generate random passwords?

相关标签:
19条回答
  • 2020-12-01 01:14

    A good password should be a mixture of both uppercase, lowercase, has number, letters, has special characters and its more than 6 characters long. Here is function I use on my apps.

    function randomPassword( $length = 8 ) 
    { 
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?"; 
    $length = rand(10, 16); 
    $password = substr( str_shuffle(sha1(rand() . time()) . $chars ), 0, $length );
     return $password;
    }
    
    0 讨论(0)
  • 2020-12-01 01:14

    This will help to create random password :

    <?php
    function generatePassword ($length = 8)
    {
      $genpassword = "";
      $possible = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
      $i = 0; 
      while ($i < $length) { 
        $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
        if (!strstr($genpassword, $char)) { 
          $genpassword .= $char;
          $i++;
        }
      }
      return $genpassword;
    } 
    ?>
    <input type="text" value="<?php echo generatePassword(); ?>">
    

    Here is the working example (Demo)

    0 讨论(0)
  • 2020-12-01 01:15

    Another, very simple (and secure!) way to do this is the following (I generate all my own passwords that way):

    base64_encode(random_bytes(12));
    

    This generates a 16 character password that uses quite a sane range of characters.

    However, depending on your requirements (for example if a user needs to type in their password), it may be desirable to remove characters that migth be confused (such as l, I, 1, 0, O, 5, S, and so on). In that case, the above solution is probably a bit too simple.

    0 讨论(0)
  • 2020-12-01 01:16

    Also you can try a function that I wrote and is available form my blog. Advantage of this function is that it gives equal importance to lowercase, uppercase, numbers and special characters. It can be found here PHP random password generator function

    0 讨论(0)
  • 2020-12-01 01:18

    I think better way to generate random password is below function, if someone want then you can use these for strong password

    public function randPassword($upper = 2, $lower = 3, $numeric = 2, $other = 1) { 
    
            $pass_order = Array(); 
            $passWord = ''; 
    
            //Create contents of the password 
            for ($i = 0; $i < $upper; $i++) { 
                $pass_order[] = chr(rand(65, 90)); 
            } 
            for ($i = 0; $i < $lower; $i++) { 
                $pass_order[] = chr(rand(97, 122)); 
            } 
            for ($i = 0; $i < $numeric; $i++) { 
                $pass_order[] = chr(rand(48, 57)); 
            } 
            for ($i = 0; $i < $other; $i++) { 
                $pass_order[] = chr(rand(33, 47)); 
            } 
    
            //using shuffle() to shuffle the order
            shuffle($pass_order); 
    
            //Final password string 
            foreach ($pass_order as $char) { 
                $passWord .= $char; 
            } 
            return $passWord; 
        } 
    
    0 讨论(0)
  • 2020-12-01 01:20

    I like to shuffle array of random chars

    $a = str_split("abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY0123456789");
    shuffle($a);
    echo implode($a); //maxlength
    echo "\n".substr( implode($a), 0, 10 ); //instead of 10 => any length
    
    //As function:
    function getMeRandomPwd($length){
        $a = str_split("abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY0123456789"); 
        shuffle($a);
        return substr( implode($a), 0, $length );
    }
    echo "\n".getMeRandomPwd(8)."\n".getMeRandomPwd(11);
    // Outpus something like:
    // 3Ai4Xf6R2I8bYGUmKgB9jpqo7ncV5teuQhkOHJCNrTP0sLFd1wxSMlEWvyaD
    // 3Ai4Xf6R2I
    // JsBKWFDa
    // gfxsjr3dX70
    

    If you need the password to be longer, just concatenate charlist a few times :)

    0 讨论(0)
提交回复
热议问题