Skip to content

Instantly share code, notes, and snippets.

@nagiyevelchin
Last active February 12, 2022 15:56
Show Gist options
  • Save nagiyevelchin/ad477f6300e9c3e849d642f07016eb52 to your computer and use it in GitHub Desktop.
Save nagiyevelchin/ad477f6300e9c3e849d642f07016eb52 to your computer and use it in GitHub Desktop.
Generate complex random password in PHP
<?php
/**
* Generate random password
* @param int $length [optional] [default 9] length password to be generated
* @return string
* @link http://www.webtoolkit.info/php-random-password-generator.html
* @since 12/1/12 11:30 AM
*/
function generatePassword($length = 9) {
$vowels = 'aeuy';
$consonants = 'bdghjmnpqrstvz';
$consonants .= 'BDGHJLMNPQRSTVWXZ';
$vowels .= "AEUY";
$consonants .= '23456789';
$consonants .= '@#$%';
$password = '';
$alt = time() % 2;
for ($i = 0; $i < $length; $i++) {
if ($alt == 1) {
$password .= $consonants[(rand() % strlen($consonants))];
$alt = 0;
} else {
$password .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
}
return $password;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment