note 92711 deleted from function.mt-rand by cmb
| From: | cmb@php.net | Date: | Sun, 19 Jan 2020 08:40:28 +0000 |
| Subject: | note 92711 deleted from function.mt-rand by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213920@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
Just another random password generator. Assembles passwords such as these:
N_v85kA2_s
8k4jz94_H0
30824n6VcN
useful for... whatever. ;)
$underscores holds the maximum allowed number of underscores. Default is two. The function is
programmed to not add an underscore at the beginning or end of the password, right after another
underscore, or if the maximum # has already been used.
$length holds the length you want the password to be. Default is 10 characters long.
<?php
function alphanumericPass()
{
// Do not modify anything below here
$underscores = 2; // Maximum number of underscores allowed in password
$length = 10; // Length of password
$p ="";
for ($i=0;$i<$length;$i++)
{
$c = mt_rand(1,7);
switch ($c)
{
case ($c<=2):
// Add a number
$p .= mt_rand(0,9);
break;
case ($c<=4):
// Add an uppercase letter
$p .= chr(mt_rand(65,90));
break;
case ($c<=6):
// Add a lowercase letter
$p .= chr(mt_rand(97,122));
break;
case 7:
$len = strlen($p);
if
($underscores>0&&$len>0&&$len<($length-1)&&$p[$len-1]!="_")
{
$p .= "_";
$underscores--;
}
else
{
$i--;
continue;
}
break;
}
}
return $p;
}
?>