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);
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology.
Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach