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进行md5加密简单实例方法

直接可以使用md5()函数,对内容进行加密,如:md5($admin_pw) 把这段密文分割成若干段,对每段都进行一次MD5运算,然后把这堆密文连成一个超长的字符串,最后再进行一次MD5...

什么情况下可以不写PHP的闭合标签“?&gt;”

在一些PHP项目里我们经常会看到有些PHP文件中的代码是只有开始标签,而没有结束标签的,那么什么情况下可以不写这个结束标签,而什么情况下又必须写? 对此我们先来看2个例子: 下面的代码可...

用PHP生成html分页列表的代码

<?php $db = mysql_connect("127.0.0.1","root","*******") or die("cant't...

php基于mcrypt的加密解密实例

本文实例讲述了php基于mcrypt实现加密解密的方法。分享给大家供大家参考。具体实现方法如下: PHP中自带了相当多的加密的方法,这里我们来看一下mcrypt扩展的使用方式。也是在工作...

解决wincache不支持64位PHP5.5/5.6的问题(提供64位wincache下载) 原创

解决wincache不支持64位PHP5.5/5.6的问题(提供64位wincache下载) 原创

这几天公司有台服务器需要配置,系统是Windows 2008 R2,在IIS上配置环境是64位的PHP5.5,要求支持wincache。原本心想无非就是去wincache的官网下载下来,...