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

Two ways to automatically generate new passwords in php

WBOY
Release: 2016-07-25 09:04:02
Original
1058 people have browsed it
  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


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template