Last active
March 26, 2020 23:21
-
-
Save smhnaji/52f2e6b2aacff6f990aedc1349de3827 to your computer and use it in GitHub Desktop.
Generate secure and readable password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function generate_password($length = 8) | |
{ | |
$sets = array(); | |
$sets[] = 'abcdefghjkmnpqrstuvwxyz'; | |
$sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ'; | |
$sets[] = '23456789'; | |
$sets[] = '!@#$^*()_.-='; | |
$all = ''; | |
$password = ''; | |
foreach($sets as $set) | |
{ | |
// Shuffle the string | |
$set = str_shuffle($set); | |
// And then select the first character of the shuffled string | |
$password .= $set[0]; | |
// Also add the string to $all (for later usage) | |
$all .= $set; | |
} | |
// For remainig character, use $all's characters, randomly | |
for($i = 0; $i < $length - count($sets); $i++) | |
{ | |
$all = str_shuffle($all); | |
$password .= $all[0]; | |
} | |
$password = str_shuffle($password); | |
return $password; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment