This function is a smart password generator, as it allows you to define the length and strength of the password. It also avoids characters that can easily be confused with digits such as lowercase 'l' and 'o'.
1. // PassStrength should be 0 to 4
2. function RandomPassword($PassLength = 6, $PassStrength = 0)
3. {
4. $Vowels = 'aeuy';
5. $Consonants = 'bdghjmnpqrstvwxz';
6. if ($PassStrength > 0)
7. {
8. $Consonants .= 'BDGHJLMNPQRSTVWXZ';
9. }
10. if($PassStrength > 1)
11. {
12. $Vowels .= "AEUY";
13. }
14. if ($PassStrength > 2)
15. {
16. $Consonants .= '23456789';
17. }
18. if ($PassStrength > 3)
19. {
20. $Consonants .= '@#$%';
21. }
22.
23. $PasswordString = "";
24. $AltChar = time() % 2;
25. srand(time());
26. for ($i = 0; $i < $PassLength; $i++)
27. {
28. if ($AltChar == 1)
29. {
30. $PasswordString .= $Consonants[(rand() % strlen($Consonants))];
31. $AltChar = 0;
32. }
33. else
34. {
35. $PasswordString .= $Vowels[(rand() % strlen($Vowels))];
36. $AltChar = 1;
37. }
38. }
39. return $PasswordString;
40. }
41.
42. // Strong password, 6 characters long
43. echo RandomPassword(6, 4);