Password Generator Function in PHP
The following GeneratePassword() function lets you to create a random password whether for your custom site or for personal use.
The function contains 1 parameter which is $len.
You can also define the set of characters "CHARS" on your parent class.
Code:
Usage:
Output:
LnxLLo+pGHiD
The function contains 1 parameter which is $len.
- $len - The number of password characters.
You can also define the set of characters "CHARS" on your parent class.
Code:
<?php
define("CHARS", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_+@(!)<>");
function GeneratePassword($len = 10)
{
$pass = ''; //Initialize random password variable
$char = str_split(CHARS);
$count = strlen(CHARS) - 1;
for ($i = 0; $i < $len; ++$i)
{
$index = rand(1, $count);
$pass .= $char[$index];
}
return $pass;
}
?>Usage:
<?php echo GeneratePassword(12); ?>
Output:
LnxLLo+pGHiD
Comments
Post a Comment