php 自定生成随机密码函数

  1. / *
  2. 用法:$ new_password = return_password();
  3. 示例:生成密码:2X5bjj2z,ERgid62Y,p2sHtDPv
  4. * /
  5. function return_password () {
  6. // set password length
  7. $pw_length = 8;
  8. // set ASCII range for random character generation
  9. $low_ascii_bound = 50; // "2"
  10. $upper_ascii_bound = 122; // "z"
  11. //开源代码phpfensi.com
  12. // Exclude special characters and some confusing alphanumerics
  13. 排除一些特殊字符和字母数字混淆
  14. // o,O,0,I,1,l etc
  15. $notuse = array (58,59,60,61,62,63,64,73,79,91,92,93,94,95,96,108,111);
  16. while ($i < $pw_length) {
  17. mt_srand ((double)microtime() * 1000000);
  18. // random limits within ASCII table
  19. $randnum = mt_rand ($low_ascii_bound, $top_ascii_bound);
  20. if (!in_array ($randnum, $notuse)) {
  21. $password = $password . chr($randnum);
  22. $i++;
  23. }
  24. }
  25. return $password;
  26. }