Skip to content

Instantly share code, notes, and snippets.

@smhnaji
Last active March 26, 2020 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smhnaji/52f2e6b2aacff6f990aedc1349de3827 to your computer and use it in GitHub Desktop.
Save smhnaji/52f2e6b2aacff6f990aedc1349de3827 to your computer and use it in GitHub Desktop.
Generate secure and readable password
<?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