php密码生成类实例_.docx

上传人:PIYPING 文档编号:11652103 上传时间:2021-08-28 格式:DOCX 页数:5 大小:12.68KB
返回 下载 相关 举报
php密码生成类实例_.docx_第1页
第1页 / 共5页
php密码生成类实例_.docx_第2页
第2页 / 共5页
php密码生成类实例_.docx_第3页
第3页 / 共5页
php密码生成类实例_.docx_第4页
第4页 / 共5页
php密码生成类实例_.docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《php密码生成类实例_.docx》由会员分享,可在线阅读,更多相关《php密码生成类实例_.docx(5页珍藏版)》请在三一文库上搜索。

1、php密码生成类实例_ 本文实例讲解并描述了php实现的密码生成类及其应用方法,分享给大家供大家参考。具体分析如下: 一、php密码生成类功能: 1.可设定密码长度。 2.可设定要生成的密码个数,批量生成。 3.可以指定密码的规章,字母,数字,特别字符等。 二、用法: GeneratePassword.class.php类文件如下: ?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970

2、7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120211122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 ?php /* Generate Password class,依据

3、指定规章生成password * Date: 2021-12-23 * Author: fdipzone * Ver: 1.0 * * Func: * public batchGenerate 批量生成密码 * private generate 生成单个密码 * private getLetter 猎取字母 * private getNumber 猎取数字 * private getSpecial 猎取特别字符 */ class GeneratePassword / class start / 密码的规章 default private $_rule = array( letter = 1,

4、number = 1, special = 1 ); private $_length = 8; / 密码长度 private $_num = 1; / 密码数量 private $_special =; /允许的特别字符 /* 初始化 * param int $length 密码长度 * param int $num 密码数量 * param Array $rule 密码规章 * param String $special 允许的特别字符 */ public function _construct($length=8, $num=1, $rule=array(), $special=) if

5、(isset($length) is_numeric($length) $length=4 $length=50) / 长度 $this-_length = $length; if(isset($num) is_numeric($num) $num0 $num=100) / 数量 $this-_num = $num; if(isset($special) is_string($special) $special!=) / 特别字符 $this-_special = $special; if($rule) / 规章 $t_rule = array(); if(isset($ruleletter)

6、 in_array($ruleletter, array(1,2,3,4,5) / 1:可选用 2:必需 3:必需小写 4:必需大写 5:大小写都必需 $t_ruleletter = $ruleletter; if(isset($rulenumber) in_array($rulenumber, array(1,2) / 1:可选用 2:必需 $t_rulenumber = $rulenumber; if(isset($rulespecial) in_array($rulespecial, array(1,2) / 1:可选用 2:必需 $t_rulespecial = $rulespecia

7、l; if($t_rule) $this-_rule = $t_rule; /* 批量生成密码 * return Array */ public function batchGenerate() $passwords = array(); for($i=0; $i$this-_num; $i+) array_push($passwords, $this-generate(); return $passwords; /* 生成单个密码 * return String */ private function generate() $password = ; $pool = ; $force_poo

8、l = ; if(isset($this-_ruleletter) $letter = $this-getLetter(); switch($this-_ruleletter) case 2: $force_pool .= substr($letter, mt_rand(0,strlen($letter)-1), 1); break; case 3: $force_pool .= strtolower(substr($letter, mt_rand(0,strlen($letter)-1), 1); $letter = strtolower($letter); break; case 4: $

9、force_pool .= strtoupper(substr($letter, mt_rand(0,strlen($letter)-1), 1); $letter = strtoupper($letter); break; case 5: $force_pool .= strtolower(substr($letter, mt_rand(0,strlen($letter)-1), 1); $force_pool .= strtoupper(substr($letter, mt_rand(0,strlen($letter)-1), 1); break; $pool .= $letter; if

10、(isset($this-_rulenumber) $number = $this-getNumber(); switch($this-_rulenumber) case 2: $force_pool .= substr($number, mt_rand(0,strlen($number)-1), 1); break; $pool .= $number; if(isset($this-_rulespecial) $special = $this-getSpecial(); switch($this-_rulespecial) case 2: $force_pool .= substr($spe

11、cial, mt_rand(0,strlen($special)-1), 1); break; $pool .= $special; $pool = str_shuffle($pool); / 随机打乱 $password = str_shuffle($force_pool. substr($pool, 0, $this-_length-strlen($force_pool); / 再次随机打乱 return $password; /* 字母 */ private function getLetter() $letter = AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpRr

12、SsTtUuVvWwXxYyZz; return $letter; /* 数字 */ private function getNumber() $number = 1234567890; return $number; /* 特别字符 */ private function getSpecial() $special = $this-_special; return $special; / class end ? demo示例程序如下: ?12345678910111213141516 ?php require GeneratePassword.class.php; $rule = array( letter = 5, / 必需含有大小写字母 number = 2, / 必需含有数字 special = 2 / 必需含有特别字符 ); $special = !#$%_-; $obj = new GeneratePassword(8, 10, $rule, $special); $passwords = $obj-batchGenerate(); echo implode(br, $passwords); ? 更多信息请查看IT技术专栏 .

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1