PHP生成隨機密碼

g6d7 9年前發布 | 2K 次閱讀 PHP 密碼

<?php
 /*
 Class MAKEpasswd:
 Make password from selected characters in a string.
 required arguments:
 length of a password and characters to use in a password.

1 = a - z 2 = A - Z 3 = a - z and A - Z 4 = a - z, A - Z and 0 - 9 5 = a - z, A - Z, 0 - 9 and chars !#$%&()

usage: Make 10 passwords that is 8 characters long and includes characters a - z, A - Z, 0 - 9 and !#$%&()

$numTimes = 0; $example = new MAKEpasswd(8,5); while($numTimes < 10) { print($example->makePassword() . "<br>\n"); $numTimes++; } */ class MAKEpasswd { var $intLength; var $pool;

    function MAKEpasswd($iLength, $iChars)
    {
            $this->intLength = $iLength;
            $this->pool = $this->getPool($iChars);
    }
    function getPool($iChars)
    {
            switch($iChars)
            {
                    case 1: /* a - z */
                            for($i = 0x61; $i <= 0x7A; $i++)
                            {
                                    $str .= chr($i);
                            }
                            return $str;
                            break;
                    case 2: /* A - Z */
                            for($i = 0x41; $i <= 0x5A; $i++)
                            {
                                    $str .= chr($i);
                            }
                            return $str;
                            break;
                    case 3: /* a - z and A - Z */
                            $str = $this->getPool(1);
                            $str .= $this->getPool(2);
                            return $str;
                            break;
                    case 4: /* 0 - 9, A - Z and a - z */
                        $str = $this->getPool(3); // get chars a - z and A - Z first
                            for($i = 0x30; $i <= 0x39; $i++)
                            {
                                    $str .= chr($i); // add chars 0 - 9;
                            }
                            return $str;
                            break;
                    case 5:
                            /* This will add these chars into the string !#$%&() */
                            $str = $this->getPool(4);
                            for($i = 0x21; $i < 0x29; $i++)
                            {
                                    if($i == 0x22 || $i == 0x27) // Exclude characters " and '
                                    {
                                            continue;
                                    }
                                    $str .= chr($i);
                            }
                            return $str;
                            break;
            }
    }
    function makePassword()
    {
            srand ((double) microtime() * 1000000);
            $str="";
            while(strlen($str)< $this->intLength)
            {
                    $str.=$this->pool[rand()%strlen($this->pool)];
            }
            return $str ;
    }

}

?></pre>

 本文由用戶 g6d7 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!