找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1136|回复: 0
打印 上一主题 下一主题

PHP生成随机密码类分享

[复制链接]

2647

主题

2647

帖子

7881

积分

论坛元老

Rank: 8Rank: 8

积分
7881
跳转到指定楼层
楼主
发表于 2018-2-14 05:57:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            类代码:
length = $length;
     
    $this->configure(true, true, true, false, false);
  }

  /**
   * 配置
   */
  function configure($uppercase = false, $lowercase = false, $number = false,
            $special = false, $extra = false
  ) {
    $this->chars = array();

    $this->upper_chars  = array(
                 "A", "B", "C", "D", "E", "F", "G", "H", "I",
                 "J", "K", "L", "M", "N", "O", "", "Q", "R",
                 "S", "T", "U", "V", "W", "X", "Y", "Z"
                );
    $this->lower_chars  = array(
                 "a", "b", "c", "d", "e", "f", "g", "h", "i",
                 "j", "k", "l", "m", "n", "o", "p", "q", "r",
                 "s", "t", "u", "v", "w", "x", "y", "z"
                );
    $this->number_chars = array(
                 "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"
                );
    $this->special_chars = array(
                 "!", "@", "#", "$", "%", "^", "&", "*", "(", ")"
                );
    $this->extra_chars  = array(
                 "[", "]", "{", "}", "-", "_", "+", "=", "", "?", "/", "`", "~", "|", ",", ".", ";", ":"
                );

    if (($this->uppercase = $uppercase) === true) {
      $this->chars = array_merge($this->chars, $this->upper_chars);
    }
    if (($this->lowercase = $lowercase) === true) {
      $this->chars = array_merge($this->chars, $this->lower_chars);
    }
    if (($this->number = $number) === true) {
      $this->chars = array_merge($this->chars, $this->number_chars);
    }
    if (($this->special = $special) === true) {
      $this->chars = array_merge($this->chars, $this->special_chars);
    }
    if (($this->extra = $extra) === true) {
      $this->chars = array_merge($this->chars, $this->extra_chars);
    }

    $this->chars = array_unique($this->chars);
  }
   
  /**
   * 从字符列中生成随机密码
   *
   * @return string
   **/
  function generate()
  {
    if (empty($this->chars)) {
      return false;
    }

    $hash    = '';
    $totalChars = count($this->chars) - 1;
     
    for ($i = 0; $i length; $i++) {
      $hash .= $this->chars[$this->random(0, $totalChars)];
    }

    return $hash;
  }

  /**
   * 生成随机数字
   *
   * @return int
   */
  function random($min = 0, $max = 0)
  {
    $max_random = 4294967295;

    $random = uniqid(microtime() . mt_rand(), true);
    $random = sha1(md5($random));

    $value = substr($random, 0, 8);
    $value = abs(hexdec($value));

    if ($max != 0) {
      $value = $min + ($max - $min + 1) * $value / ($max_random + 1);
    }

    return abs(intval($value));
  }
}
调用:
generate();

//FS4yq74e2LeE
            
            
您可能感兴趣的文章:
  • php生成随机密码的三种方法小结
  • php生成随机密码的几种方法
  • PHP生成随机用户名和密码的实现代码
  • php中生成随机密码的自定义函数代码
  • php生成随机密码自定义函数代码(简单快速)
  • php实现随机生成易于记忆的密码
  • PHP生成随机密码方法汇总
  • 纯php生成随机密码
  • PHP中快速生成随机密码的几种方式
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端