PHP生成随机密码

前端之家收集整理的这篇文章主要介绍了PHP生成随机密码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

<?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,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 ;
        }
 }

 ?>

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

原文链接:https://www.f2er.com/php/462738.html

猜你在找的PHP相关文章