Function to generate random passwords


/ Published in: PHP
Save to your folder(s)

A function to create passwords for new users or password resets.


Copy this code and paste it in your HTML
  1. /* a function to create passwords for new users or password resets. standard
  2. disclaimers apply. not promised to work. take the code as-is. if you like, find
  3. errors, or use please let me know at luckygreentiger at gmail */
  4.  
  5. function randomPassword($maxLength) {
  6. $possible = "#0123456789+bBcCdDfFgGhHjJkKmMnNpPqQrRsStTvVwWxXyYzZ-";
  7.  
  8. if($maxLength == "") {
  9. $maxLength = 12;
  10. }
  11. while(($beat < $maxLength) && (strlen($possible) > 0)) {
  12. $beat++;
  13. // get rand character from possibles
  14. $character = substr($possible, mt_rand(0, strlen($possible)-1), 1);
  15. // delete selected char from possible choices
  16. $possible = str_replace($character, "", $possible);
  17. $password .= $character;
  18. }
  19. return $password;
  20. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.