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 26 PHP
php控制linux服务器常用功能 关机 重启 开新站点等
Sep 05 PHP
PHP简单选择排序算法实例
Jan 26 PHP
php+mysqli数据库连接的两种方式
Jan 28 PHP
PHP 实现代码复用的一个方法 traits新特性
Feb 22 PHP
php实现对两个数组进行减法操作的方法
Apr 17 PHP
护卫神php套件 php版本升级方法(php5.5.24)
May 10 PHP
smarty的section嵌套循环用法示例
May 28 PHP
PHP接口并发测试的方法(推荐)
Dec 15 PHP
PHP实现登录注册之BootStrap表单功能
Sep 03 PHP
laravel框架邮箱认证实现方法详解
Nov 22 PHP
PHP设计模式(六)桥连模式Bridge实例详解【结构型】
May 02 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
第十三节 对象串行化 [13]
2006/10/09 PHP
用PHP获取Google AJAX Search API 数据的代码
2010/03/12 PHP
PHP+Mysql+jQuery实现动态展示信息
2011/10/08 PHP
PHP中最容易忘记的一些知识点总结
2013/04/28 PHP
php判断类是否存在函数class_exists用法分析
2014/11/14 PHP
PHP编写学校网站上新生注册登陆程序的实例分享
2016/03/21 PHP
微信公众平台DEMO(PHP)
2016/05/04 PHP
PHP如何获取当前主机、域名、网址、路径、端口等参数
2017/06/09 PHP
PHP微信支付结果通知与回调策略分析
2019/01/10 PHP
非常不错的功能强大代码简单的管理菜单美化版
2008/07/09 Javascript
JS 实现双色表格实现代码
2009/11/24 Javascript
Javascript对象中关于setTimeout和setInterval的this介绍
2012/07/21 Javascript
利用毫秒减值计算时长的js代码
2013/09/22 Javascript
jquery 取子节点及当前节点属性值的方法
2014/08/24 Javascript
认识Knockout及如何使用Knockout绑定上下文
2015/12/25 Javascript
JS仿淘宝搜索框用户输入事件的实现
2017/06/19 Javascript
使用JavaScript实现点击循环切换图片效果
2017/09/03 Javascript
vue按需引入element Transfer 穿梭框
2017/09/30 Javascript
基于Require.js使用方法(总结)
2017/10/26 Javascript
详解一个小实例理解js原型和继承
2019/04/24 Javascript
JavaScript中AOP的实现与应用
2019/05/06 Javascript
RxJS在TypeScript中的简单使用详解
2020/04/13 Javascript
解决antd的Form组件setFieldsValue的警告问题
2020/10/29 Javascript
手写Vue2.0 数据劫持的示例
2021/03/04 Vue.js
python练习程序批量修改文件名
2014/01/16 Python
Python进行数据科学工作的简单入门教程
2015/04/01 Python
Python中的 enum 模块源码详析
2019/01/09 Python
Python实现K折交叉验证法的方法步骤
2019/07/11 Python
TensorFlow索引与切片的实现方法
2019/11/20 Python
Python动态强类型解释型语言原理解析
2020/03/25 Python
python实现Pyecharts实现动态地图(Map、Geo)
2020/03/25 Python
简单了解python调用其他脚本方法实例
2020/03/26 Python
tensorflow转换ckpt为savermodel模型的实现
2020/05/25 Python
Python numpy矩阵处理运算工具用法汇总
2020/07/13 Python
2015年七夕情人节感言
2015/08/03 职场文书
哪类餐饮行业,最适合在高校创业?
2019/08/19 职场文书