Last Updated: February 25, 2016
·
441
· wisnu

password generator using 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;
}

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#php
#password
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside