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字符串转义相关的配置和函数如下: 1.magic_quotes_runtime 2.magic_quotes_gpc 3....

RSA实现JS前端加密与PHP后端解密功能示例

RSA实现JS前端加密与PHP后端解密功能示例

本文实例讲述了RSA实现JS前端加密与PHP后端解密功能。分享给大家供大家参考,具体如下: web前端,用户注册与登录,不能直接以明文形式提交用户密码,容易被截获,这时就引入RSA。 前...

简单实现php上传文件功能

本文实例为大家分享了php上传文件功能的具体代码,供大家参考,具体内容如下 html: <form action="upload_file.php" method="post"...

PHP 年龄计算函数(精确到天)

复制代码 代码如下: <?php /** * PHP 年龄计算函数 * * 参数支持数组传参和标准的 Mysql date 类型传参 * params sample * -----...

PHP开发的微信现金红包功能示例

本文实例讲述了PHP开发的微信现金红包功能。分享给大家供大家参考,具体如下: 微信商家后台-现金红包开发 sdk <?php class wxPay { //配置参...