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

yipeiwu_com5年前PHP代码库

本文实例讲述了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

PHP学习笔记(一) 简单了解PHP

目标规划: 通过第一节课,我们可以了解php环境. 1.环境的认识: 2.访问方法: 3.修改代码及查看. 4.变量的使用 5.代码缩进要有层次关系,而且代码之间最好保留空行 6.变量命...

PHP中的use关键字概述

很多开源系统如osCommerce框架中,都会在其源码中找到use这个关键字,如osCommerce框架中就在index.php文件中出现了这段源码: use osCommerce\...

PHP中call_user_func_array回调函数的用法示例

call_user_func_array call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数 mixed call_user_func_a...

自己写了一个php检测文件编码的函数

关于文件编码的检测,百度一下一大把都是,但是确实没有能用的、 很多人建议 mb_detect_encoding 检测,可是不知为何我这不成功,什么都没输出、 看到有人写了个增强版,用 B...

php获取微信openid方法总结

使用微信接口,无论是自动登录还是微信支付我们首先需要获取的就是openid,获取openid的方式有两种,一种是在关注的时候进行获取,这种订阅号就可以获取的到,第二种是通过网页授权获取,...