摘要:本文主要向大家介绍了PHP语言 密码生成类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
本文主要向大家介绍了PHP语言 密码生成类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
功能:
1.可设定密码长度。
2.可设定要生成的密码个数,批量生成。
3.可以指定密码的规则,字母,数字,特殊字符等。
GeneratePassword.class.php
[php] view plain copy
1. <?php
2. /** Generate Password class,根据指定规则生成password
3. * Date: 2013-12-23
4. * Author: fdipzone
5. * Ver: 1.0
6. *
7. * Func:
8. * public batchGenerate 批量生成密码
9. * private generate 生成单个密码
10. * private getLetter 获取字母
11. * private getNumber 获取数字
12. * private getSpecial 获取特殊字符
13. */
14.
15. class GeneratePassword{ // class start
16.
17. // 密码的规则 default
18. private $_rule = array(
19. 'letter' => 1,
20. 'number' => 1,
21. 'special' => 1
22. );
23.
24. private $_length = 8; // 密码长度
25. private $_num = 1; // 密码数量
26. private $_special = '!@#$%^&*()_+=-'; //允许的特殊字符
27.
28.
29. /** 初始化
30. * @param int $length 密码长度
31. * @param int $num 密码数量
32. * @param Array $rule 密码规则
33. * @param String $special 允许的特殊字符
34. */
35. public function __construct($length=8, $num=1, $rule=array(), $special=''){
36.
37. if(isset($length) && is_numeric($length) && $length>=4 && $length<=50){ // 长度
38. $this->_length = $length;
39. }
40.
41. if(isset($num) && is_numeric($num) && $num>0 && $num<=100){ // 数量
42. $this->_num = $num;
43. }
44.
45. if(isset($special) && is_string($special) && $special!=''){ // 特殊字符
46. $this->_special = $special;
47. }
48.
49. if($rule){ // 规则
50.
51. $t_rule = array();
52.
53. if(isset($rule['letter']) && in_array($rule['letter'], array(1,2,3,4,5))){ // 1:可选用 2:必须 3:必须小写 4:必须大写 5:大小写都必须
54. $t_rule['letter'] = $rule['letter'];
55. }
56.
57. if(isset($rule['number']) && in_array($rule['number'], array(1,2))){ // 1:可选用 2:必须
58. $t_rule['number'] = $rule['number'];
59. }
60.
61. if(isset($rule['special']) && in_array($rule['special'], array(1,2))){ // 1:可选用 2:必须
62. $t_rule['special'] = $rule['special'];
63. }
64.
65. if($t_rule){
66. $this->_rule = $t_rule;
67. }
68.
69. }
70.
71. }
72.
73.
74. /** 批量生成密码
75. * @return Array
76. */
77. public function batchGenerate(){
78.
79. $passwords = array();
80.
81. for($i=0; $i<$this->_num; $i++){
82. array_push($passwords, $this->generate());
83. }
84.
85. return $passwords;
86. }
87.
88.
89. /** 生成单个密码
90. * @return String
91. */
92. private function generate(){
93.
94. $password = '';
95. $pool = '';
96. $force_pool = '';
97.
98. if(isset($this->_rule['letter'])){
99.
100. $letter = $this->getLetter();
101.
102. switch($this->_rule['letter']){
103. case 2:
104. $force_pool .= substr($letter, mt_rand(0,strlen($letter)-1), 1);
105. break;
106.
107. case 3:
108. $force_pool .= strtolower(substr($letter, mt_rand(0,strlen($letter)-1), 1));
109. $letter = strtolower($letter);
110. break;
111.
112. case 4:
113. $force_pool .= strtoupper(substr($letter, mt_rand(0,strlen($letter)-1), 1));
114. $letter = strtoupper($letter);
115. break;
116.
117. case 5:
118. $force_pool .= strtolower(substr($letter, mt_rand(0,strlen($letter)-1), 1));
119. $force_pool .= strtoupper(substr($letter, mt_rand(0,strlen($letter)-1), 1));
120. break;
121. }
122.
123. $pool .= $letter;
124.
125. }
126.
127. if(isset($this->_rule['number'])){
128.
129. $number = $this->getNumber();
130.
131. switch($this->_rule['number']){
132. case 2:
133. $force_pool .= substr($number, mt_rand(0,strlen($number)-1), 1);
134. break;
135. }
136.
137. $pool .= $number;
138.
139. }
140.
141. if(isset($this->_rule['special'])){
142.
143. $special = $this->getSpecial();
144.
145. switch($this->_rule['special']){
146. case 2:
147. $force_pool .= substr($special, mt_rand(0,strlen($special)-1), 1);
148. break;
149. }
150.
151. $pool .= $special;
152. }
153.
154. $pool = str_shuffle($pool); // 随机打乱
155.
156. $password = str_shuffle($force_pool. substr($pool, 0, $this->_length-strlen($force_pool))); // 再次随机打乱
157.
158. return $password;
159.
160. }
161.
162.
163. /** 字母 */
164. private function getLetter(){
165. $letter = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz';
166. return $letter;
167. }
168.
169.
170. /** 数字 */
171. private function getNumber(){
172. $number = '1234567890';
173. return $number;
174. }
175.
176.
177. /** 特殊字符 */
178. private function getSpecial(){
179. $special = $this->_special;
180. return $special;
181. }
182.
183. } // class end
184.
185. ?>
demo:
[php] view plain copy
1. <?php
2. require 'GeneratePassword.class.php';
3.
4. $rule = array(
5. 'letter' => 5, // 必须含有大小写字母
6. 'number' => 2, // 必须含有数字
7. 'special' => 2 // 必须含有特殊字符
8. );
9.
10. $special = '!@#$%_-';
11.
12. $obj = new GeneratePassword(8, 10, $rule, $special);
13. $passwords = $obj->batchGenerate();
14.
15. echo implode('<br>', $passwords);
16. ?>
以上就介绍了PHP的相关知识,希望对PHP有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言PHP频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号