PHP random password generation of custom functions

Source: Internet
Author: User
Tags chr generator strlen valid

You can specify the length of the generated string

function Rand_str ($length, $max =false)
{
if (Is_int ($max) && $max > $length)
{
$length = Mt_rand ($length, $max);
}
$output = ';

for ($i =0; $i < $length; $i + +)
{
$which = Mt_rand (0,2);

if ($which = = 0)
{
$output. = Mt_rand (0,9);
}
ElseIf ($which = = 1)
{
$output. = Chr (Mt_rand (65,90));
}
Else
{
$output. = Chr (Mt_rand (97,122));
}
}
return $output;
}
Invoke instance:
$randstr = Rand_str (16);

Functions that generate random strings

<?php
/**
* Generate random string
*
* produces a random string of a specified length and returns it to the user
*
* @access Public
* @param int $len The number of digits that produce a string
* @return String
*/
function Randstr ($len =6) {
$chars = ' abdefghjklmnpqrstvwxyabdefghijkmnpqrstvwxy23456789#%* '; Characters to builds the password from
Mt_srand (Double) Microtime () *1000000*getmypid ()); Seed the random number generater (must is done)
$password = ';
while (strlen ($password) < $len)
$password. =substr ($chars, (Mt_rand ()%strlen ($chars)), 1);
return $password;
}
?>


Create a character Fu Chi.


Function Randomkeys ($length)
   {
       $pattern = ' 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ
                    abcdefghijklomnopqrstuvwxyz,./&amp;l
                   t;&gt;?; #:@~[]{}-_=+) (*&amp;^%$?! ';    //character Fu Chi
      for ($i =0 $i < $length; $i + +)
        {
           $key. = $pattern {Mt_rand (0,35 )};   //Generate PHP random number
      }
        return $key;
  }
   Echo Randomkeys (8);


no need to create character Fu Chi

function Randomkeys ($length)
{
$output = ';
for ($a = 0; $a < $length; $a + +) {
$output. = Chr (Mt_rand (35, 126)); Generate PHP Random number
}
return $output;
}
Echo Randomkeys (8);


random user name and random password example

Randomly generated user name (length 6-13)

function Create_password ($PW _length = 4) {
$randpwd = ';
for ($i = 0; $i < $PW _length; $i + +) {
$randpwd. = Chr (Mt_rand (33, 126));
}
return $randpwd;
}

function Generate_username ($length = 6) {
Password character set, you can add any character you need
$chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&* ()-_ []{}<>~ ' +=,.;:/?| ';
$password = ';
for ($i = 0; $i < $length; $i + +)
{
Here are two ways to get a character
The first is to use SUBSTR to intercept any one character in the $chars;
The second is any element that takes the $chars of a character array
$password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1);
$password. = $chars [Mt_rand (0, strlen ($chars)-1)];
}
return $password;
}

Call
$userId = ' user '. Generate_username (6);
$pwd = Create_password (9);

Mt_srand generate random seeds, the length of the password can be defined arbitrarily, the longest 32 bits.

<?php
Mt_srand (Double) microtime () * 1000000);

function Gen_random_password ($password _length = $generated _password = "") {
$valid _characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$chars _length = strlen ($valid _characters)-1;
for ($i = $password _length; $i-;) {
$generated _password. = $valid _characters[mt_rand (0, $chars _length)];

$generated _password. = substr ($valid _characters, (Mt_rand ()% (strlen ($valid _characters)), 1);
}
return $generated _password;
}
?>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<title>php Password Generator </title>
<style type= "Text/css" >
Body {
font-family:arial;
font-size:10pt;
}
</style>
<body>
<span style= "Font-weight:bold; font-size:15pt; " > Password generator </span><br/><br/>
<?php

if (Isset ($_get[' password_length ')) {
if (Preg_match ("/([0-9]{1,8})/", $_get[' Password_length ')) {
Print ("Password generation succeeded: <br/>
<span style= "Font-weight:bold" > ". Gen_random_password ($_get[' password_length ')). "</span><br/><br/>n");
} else {
Print ("Incorrect password length!<br/><br/>n");
}
}

Print <<< End
Please generate the length of its specified build password for the password: <br/><br/>
<form action= "{$_server[' php_self ']}" method= "get" >
<input type= "text" name= "Password_length" >
<input type= "Submit" value= "Build" >
</form>
End
?>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.