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 09 PHP
用PHP实现多级树型菜单
Oct 09 PHP
php strstr查找字符串中是否包含某些字符的查找函数
Jun 03 PHP
zf框架的数据库追踪器使用示例
Mar 13 PHP
兼容PHP和Java的des加密解密代码分享
Jun 26 PHP
php中fsockopen用法实例
Jan 05 PHP
PHP记录搜索引擎蜘蛛访问网站足迹的方法
Apr 15 PHP
php使用COPY函数更新配置文件的方法
Jun 18 PHP
Yii2配置Nginx伪静态的方法
May 05 PHP
PHP 实现页面静态化的几种方法
Jul 23 PHP
PHP基于phpqrcode类生成二维码的方法示例详解
Aug 07 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
2020年4月新番动漫目录 官方宣布4月播出的作品一览
2020/03/08 日漫
Apache2 httpd.conf 中文版
2006/11/17 PHP
PHP面向对象分析设计的经验原则
2008/09/20 PHP
使用PHP遍历文件目录与清除目录中文件的实现详解
2013/06/24 PHP
解析PHP中的内存管理,PHP动态分配和释放内存
2013/06/28 PHP
PHP开发注意事项总结
2015/02/04 PHP
php使用Session和文件统计在线人数
2015/07/04 PHP
Yii2框架中日志的使用方法分析
2017/05/22 PHP
PHP对称加密算法(DES/AES)类的实现代码
2017/11/14 PHP
Laravel下生成验证码的类
2017/11/15 PHP
javascript 添加和移除函数的通用方法
2009/10/20 Javascript
Visual Studio中的jQuery智能提示设置方法
2010/03/27 Javascript
JavaScript 弹出窗体点击按钮返回选择数据的实现
2010/04/01 Javascript
解决3.01版的jquery.form.js中文乱码问题的解决方法
2012/03/08 Javascript
js确定对象类型方法
2012/03/30 Javascript
通过jQuery源码学习javascript(一)
2012/12/27 Javascript
在javascript中实现函数数组的方法
2013/12/25 Javascript
超级简单实现JavaScript MVC 样式框架
2015/03/24 Javascript
JavaScript实现彩虹文字效果的方法
2015/04/16 Javascript
jQuery团购倒计时特效实现方法
2015/05/07 Javascript
jQuery操作Table技巧大汇总
2016/01/23 Javascript
Bootstrap CDN和本地化环境搭建
2016/10/26 Javascript
jquery利用json实现页面之间传值的实例解析
2016/12/12 Javascript
如何使用VuePress搭建一个类型element ui文档
2019/02/14 Javascript
CKEditor 4.4.1 添加代码高亮显示插件功能教程【使用官方推荐Code Snippet插件】
2019/06/14 Javascript
微信小程序select下拉框实现源码
2019/11/08 Javascript
详解python3中zipfile模块用法
2018/06/18 Python
python实现RabbitMQ的消息队列的示例代码
2018/11/08 Python
python pygame模块编写飞机大战
2018/11/20 Python
Python 调用 zabbix api的方法示例
2019/01/06 Python
python连接打印机实现打印文档、图片、pdf文件等功能
2020/02/07 Python
Python基于类路径字符串获取静态属性
2020/03/12 Python
Python selenium实现断言3种方法解析
2020/09/08 Python
css3利用transform变形结合事件完成扇形导航
2020/10/26 HTML / CSS
市场专员岗位职责
2014/02/14 职场文书
学校党员干部承诺书
2015/05/04 职场文书