Home  >  Article  >  Backend Development  >  Two ways to automatically generate new passwords in php

Two ways to automatically generate new passwords in php

WBOY
WBOYOriginal
2016-07-25 09:04:02987browse
  1. /*
  2. * php automatically generates a new password custom function
  3. Applicable environment: PHP5.2.x / mysql 5.0.x
  4. Code author: xujiajay
  5. * */
  6. function genPassword($ min = 5, $max = 8)
  7. {
  8. $validchars="abcdefghijklmnopqrstuvwxyz123456789";
  9. $max_char=strlen($validchars)-1;
  10. $length=mt_rand($min,$max);
  11. $password = "" ;
  12. for($i=0;$i<$length;$i )
  13. {
  14. $password.=$validchars[mt_rand(0,$max_char)];
  15. }
  16. return $password;
  17. }
  18. echo "New Password: ".genPassword()."
    ";
  19. echo "New password: ".genPassword(5,10)."
    ";
  20. ?>
Copy code

Method 2:

  1. /**

  2. php generate random password
  3. */
  4. function generatePassword($length=8)
  5. {
  6. $chars = array_merge(range(0,9),
  7. range ('a','z'),
  8. range('A','Z'),
  9. array('!','@','$','%','^','&',' *'));
  10. shuffle($chars);
  11. $password = '';
  12. for($i=0; $i<8; $i++) {
  13. $password .= $chars[$i];
  14. }
  15. return $password;
  16. }

  17. echo "New password:" . generatePassword(16);

  18. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn