php实现随机生成易于记忆的密码


Posted in PHP onJune 19, 2015

本文实例讲述了php实现随机生成易于记忆的密码。分享给大家供大家参考。具体实现方法如下:

这里通过预定义一些单词,让php随机从这些单词中选择进行组合生成密码

function random_readable_pwd($length=10){
  // the wordlist from which the password gets generated 
  // (change them as you like)
  $words = 'dog,cat,sheep,sun,sky,red,ball,happy,ice,';
  $words .= 'green,blue,music,movies,radio,green,turbo,';
  $words .= 'mouse,computer,paper,water,fire,storm,chicken,';
  $words .= 'boot,freedom,white,nice,player,small,eyes,';
  $words .= 'path,kid,box,black,flower,ping,pong,smile,';
  $words .= 'coffee,colors,rainbow,plus,king,tv,ring';
  // Split by ",":
  $words = explode(',', $words);
  if (count($words) == 0){ die('Wordlist is empty!'); }
  // Add words while password is smaller than the given length
  $pwd = '';
  while (strlen($pwd) < $length){
    $r = mt_rand(0, count($words)-1);
    $pwd .= $words[$r];
  }
  // append a number at the end if length > 2 and
  // reduce the password size to $length
  $num = mt_rand(1, 99);
  if ($length > 2){
    $pwd = substr($pwd,0,$length-strlen($num)).$num;
  } else { 
    $pwd = substr($pwd, 0, $length);
  }
  return $pwd;
}
//使用范例:
random_readable_pwd(10) => returns something like: pingwater6, radiohap28, sunwhite84, happykid44, etc...

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
十天学会php之第九天
Oct 09 PHP
php 魔术方法使用说明
Oct 20 PHP
PhpMyAdmin中无法导入sql文件的解决办法
Jan 08 PHP
基于PHP读取TXT文件向数据库导入海量数据的方法
Apr 23 PHP
php分页示例分享
Apr 30 PHP
php中in_array函数用法分析
Nov 15 PHP
PHP使用pear自带的mail类库发邮件的方法
Jul 08 PHP
在Mac OS上编译安装Nginx+PHP+MariaDB开发环境的教程
Feb 23 PHP
PHP 验证身份证是否合法的函数
Feb 09 PHP
php ajax confirm 删除实例详解
Mar 06 PHP
php中目录操作opendir()、readdir()及scandir()用法示例
Jun 08 PHP
laravel使用redis队列实例讲解
Mar 23 PHP
php根据一个给定范围和步进生成数组的方法
Jun 19 #PHP
php分割合并两个字符串的函数实例
Jun 19 #PHP
php计算整个mysql数据库大小的方法
Jun 19 #PHP
php判断访问IP的方法
Jun 19 #PHP
PHP自动生成表单代码分享
Jun 19 #PHP
PHP实现中文圆形印章特效
Jun 19 #PHP
PHP 常用的header头部定义汇总
Jun 19 #PHP
You might like
PHP生成唯一的促销/优惠/折扣码(附源码)
2012/12/28 PHP
PHP中数据库单例模式的实现代码分享
2014/08/21 PHP
在html文件中也可以执行php语句的方法
2015/04/09 PHP
thinkPHP使用pclzip打包备份mysql数据库的方法
2016/04/30 PHP
PHP实现中国公民身份证号码有效性验证示例代码
2017/05/03 PHP
visual studio code 调试php方法(图文详解)
2017/09/15 PHP
JavaScript 基础篇之对象、数组使用介绍(三)
2012/04/07 Javascript
圣诞节Merry Christmas给博客添加浪漫的下雪效果基于jquery实现
2012/12/27 Javascript
THREE.JS入门教程(4)创建粒子系统
2013/01/24 Javascript
javascript 获取浏览器版本
2015/01/21 Javascript
为什么JavaScript没有块级作用域
2016/05/22 Javascript
全面解析Bootstrap中form、navbar的使用方法
2016/05/30 Javascript
JS正则表达式修饰符中multiline(/m)用法分析
2016/12/27 Javascript
vue实现抖音时间转盘
2019/09/08 Javascript
微信小程序swiper实现文字纵向轮播提示效果
2020/01/21 Javascript
vue-cli点击实现全屏功能
2020/03/07 Javascript
maptalks+three.js+vue webpack实现二维地图上贴三维模型操作
2020/08/10 Javascript
kNN算法python实现和简单数字识别的方法
2014/11/18 Python
Python 对象中的数据类型
2017/05/13 Python
Python读取txt文件数据的方法(用于接口自动化参数化数据)
2018/06/27 Python
Python格式化输出字符串方法小结【%与format】
2018/10/29 Python
django搭建项目配置环境和创建表过程详解
2019/07/22 Python
python实现的发邮件功能示例
2019/09/11 Python
Python爬虫使用浏览器cookies:browsercookie过程解析
2019/10/22 Python
如何更改 pandas dataframe 中两列的位置
2019/12/27 Python
CSS3模拟IOS滑动开关效果
2016/09/28 HTML / CSS
Fossil美国官网:Fossil手表、手袋、珠宝及配件
2017/02/01 全球购物
美国儿童玩具、装扮和玩偶商店:Magic Cabin
2018/09/02 全球购物
复古服装:RetroStage
2019/05/10 全球购物
财务人员个人自荐信范文
2013/09/26 职场文书
信息专业毕业生五年职业规划参考
2014/02/06 职场文书
办公室文员岗位职责
2015/02/04 职场文书
干部培训工作总结2015
2015/05/25 职场文书
爱的教育读书笔记
2015/06/26 职场文书
2016感恩母亲节校园广播稿
2015/12/17 职场文书
【2·13】一图读懂中国无线电发展
2022/02/18 无线电