Skip to content

Instantly share code, notes, and snippets.

@Sn4kY
Last active June 5, 2018 13:00
Show Gist options
  • Save Sn4kY/d6379a3177af54d46ec2fa0fbccca7d8 to your computer and use it in GitHub Desktop.
Save Sn4kY/d6379a3177af54d46ec2fa0fbccca7d8 to your computer and use it in GitHub Desktop.
PHP script to generate password. Call url without any query string to get a medium strength password with 15 char width, or with "length=XXX" for more char, or/and with "nocomplex" OR "complex" : http://example.com/password_generate.php?length=30&complex
<?php
function rand_str($size, $complex)
{
// A-Z a-z 0-9
$source = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));
$password = "";
if ($complex >= 1) {
$source = array_merge($source, str_split('.-_'));
}
if ($complex >= 2) {
$source = array_merge($source, str_split('!?,;:@&/%$#'));
}
for ($i = 0; $i < abs(intval($size)); $i++) {
$password .= $source[rand(0, sizeof($source) - 1)];
}
return $password;
}
$defaultLength = 15;
$length = isset($_GET['length']) ? abs((int)$_GET['length']) : $defaultLength;
$strength = 1;
$strength = isset($_GET['complex']) ? 2 : $strength;
$strength = isset($_GET['nocomplex']) ? 0 : $strength;
// Password length control
if ($length < 0 || $length > 1000) {
$length = $defaultLength;
}
echo rand_str($length, $strength);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment