Home >Backend Development >PHP Tutorial >PHP randomly generates easy-to-remember password example code

PHP randomly generates easy-to-remember password example code

怪我咯
怪我咯Original
2017-07-11 11:54:521264browse

Password is a technique used to obfuscate the user's desire to transform normal (identifiable) information into unrecognizable information. But parts of this unrecognizable information can be reprocessed and recovered and deciphered. Password is the general name for "password" in Chinese. The "password" entered when logging into websites, e-mails, and bank withdrawals should strictly speaking be called "passwords" because it is not an "encryption code" in the original sense, but it can also be called a secret number. It is mainly limited to a system of symbols that an individual understands (such as a message). Such as cipher telegraph and cipher typewriter.

This article mainly introduces the implementation of PHP to randomly generate easy-to-remember passwords. It analyzes the related techniques of PHP to generate random passwords with examples. Friends in need can refer to it.

Here are some predefined words. , let php randomly select and combine from these words to generate the password

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...


The above is the detailed content of PHP randomly generates easy-to-remember password example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn