PHP 8.5.0 Beta 1 available for testing

Voting

: zero plus two?
(Example: nine)

The Note You're Voting On

scott at quinlan dot co dot nz
16 years ago
Secure password generator with a variable maximum amount of symbols.

<?php

function passwdGen($minLength = 8, $maxLength = 12, $maxSymbols = 2)
{
$symbolCount = 0;

srand((double)microtime() * 1000003);

for (
$i = 0; $i < rand($minLength, $maxLength); $i++)
{
do
{
$char = rand(33, 126);

$symbolCount += $isSymbol = (!in_array($char, range(48, 57)) && !in_array($char, range(65, 90)) && !in_array($char, range(97, 122)));

if (
$symbolCount <= $maxSymbols || !$isSymbol)
{
break;
}
}
while (
true);

$passwd = sprintf('%s%c', isset($passwd) ? $passwd : NULL, $char);
}

return
$passwd;
}

?>

<< Back to user notes page

To Top