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 cli 小技巧
Jun 03 PHP
探讨:array2xml和xml2array以及xml与array的互相转化
Jun 24 PHP
PHP中对各种加密算法、Hash算法的速度测试对比代码
Jul 08 PHP
PHP基于数组实现的分页函数实例
Aug 20 PHP
php实现比较全的数据库操作类
Jun 18 PHP
PHP编写RESTful接口
Feb 23 PHP
Zend Framework教程之Zend_Db_Table表关联实例详解
Mar 23 PHP
Yii统计不同类型邮箱数量的方法
Oct 18 PHP
利用PHPExcel实现Excel文件的写入和读取
Apr 26 PHP
Windows平台实现PHP连接SQL Server2008的方法
Jul 26 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生成图片验证码
2015/06/09 PHP
PHP实现断点续传乱序合并文件的方法
2018/09/06 PHP
jquery实现表单验证并阻止非法提交
2015/07/09 Javascript
node.js使用cluster实现多进程
2016/03/17 Javascript
详解Node.js项目APM监控之New Relic
2017/05/12 Javascript
微信小程序联网请求的轮播图
2017/07/07 Javascript
浅谈Angular文字折叠展开组件的原理分析
2017/11/24 Javascript
VSCode中如何利用d.ts文件进行js智能提示
2018/04/13 Javascript
jQuery实现的手动拖动控制进度条效果示例【测试可用】
2018/04/18 jQuery
vue项目从node8.x升级到12.x后的问题解决
2019/10/25 Javascript
JS实现前端路由功能示例【原生路由】
2020/05/29 Javascript
JavaScript编写开发动态时钟
2020/07/29 Javascript
Python常见数据结构详解
2014/07/24 Python
python实现简单ftp客户端的方法
2015/06/28 Python
python写日志封装类实例
2015/06/28 Python
python使用arcpy.mapping模块批量出图
2017/03/06 Python
Python中selenium实现文件上传所有方法整理总结
2017/04/01 Python
解决python文件字符串转列表时遇到空行的问题
2017/07/09 Python
python回调函数中使用多线程的方法
2017/12/25 Python
Python数据可视化 pyecharts实现各种统计图表过程详解
2019/08/15 Python
Django 简单实现分页与搜索功能的示例代码
2019/11/07 Python
python读取xml文件方法解析
2020/08/04 Python
Numpy ndarray 多维数组对象的使用
2021/02/10 Python
北美主要的汽车零部件零售商:AutoShack.com
2019/02/23 全球购物
英国最大的在线蜡烛商店:Candles Direct
2019/03/26 全球购物
Belstaff英国官方在线商店:Belstaff.co.uk
2021/02/09 全球购物
XML文档面试题
2015/08/05 面试题
事业单位请假制度
2014/01/13 职场文书
小学家长会邀请函
2014/01/23 职场文书
我读书我快乐演讲稿
2014/05/07 职场文书
组织生活会表态发言材料
2014/10/17 职场文书
加强作风建设心得体会
2014/10/22 职场文书
生日答谢词
2015/01/05 职场文书
求职导师推荐信范文
2015/03/27 职场文书
讲座通知范文
2015/04/23 职场文书
在虚拟机中安装windows server 2008的图文教程
2022/06/28 Servers