PHP生成随机密码类分享_php实例

php中文网
发布: 2016-05-17 08:40:48
原创
1274人浏览过

类代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

<&#63;php

/**

 * PHP - Password Generator Class

 * Version 1.0.0

 *

 */

  

if (@!is_object($passGen) || !isset($passGen)) {

  $passGen = new Password;

}

  

class Password

{

  

  /**

   * 大写字母 A-Z

   *

   * @var array

   */

  protected $uppercase_chars;

  

  /**

   * 小写字母 a-z

   *

   * @var array

   */

  protected $lowercase_chars;

  

  /**

   * 阿拉伯数字 0-9

   *

   * @var array

   */

  protected $number_chars;

  

  /**

   * 特殊字符

   *

   * @var array

   */

  protected $special_chars;

  

  /**

   * 其他特殊字符

   *

   * @var array

   */

  protected $extra_chars;

  

  /**

   * 最终用来生成密码的所有字符

   *

   * @var array

   */

  protected $chars = array();

  

  /**

   * 密码长度

   *

   * @var array

   */

  public $length;

  

  /**

   * 是否使用大写字母

   *

   * @var boolean

   */

  public $uppercase;

  

  /**

   * 是否使用小写字母

   *

   * @var boolean

   */

  public $lowercase;

  

  /**

   * 是否使用阿拉伯数字

   *

   * @var boolean

   */

  public $number;

  

  /**

   * 是否使用特殊字符

   *

   * @var boolean

   */

  public $special;

  

  /**

   * 是否使用额外的特殊字符

   *

   * @var boolean

   */

  public $extra;

  

  /**

   * 初始化密码设置

   *

   * @param int $length

   */

  function Password($length = 12)

  {

    $this->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", "P", "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(

                 "[", "]", "{", "}", "-", "_", "+", "=", "<",

                 ">", "&#63;", "/", "`", "~", "|", ",", ".", ";", ":"

                );

  

    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 < $this->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));

  }

}

登录后复制

调用:

1

2

3

4

5

6

7

<&#63;php

  

include_once 'password.class.php';

  

echo $passGen->generate();

  

//FS4yq74e2LeE

登录后复制

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号