Passwords
|
|
|
|
<html>
<head>
<title>Simple Password Generator</title>
<!-- PHP-Code start //-->
<?php /****************
Title: Simple Password Generator with parameters
Author: TheSun
E-Mail: apocataclysm@hotmail.com
Description: (see title)
Generates a (pseudo)-random password based on following parameters:
- Length: How long the password should be
- Number of numbers: How many numbers should be included in the password
(cannot be longer than length)
License: Free to use for anyone. Please just leave all comments intact.
*****************/
$password=""; $len=8; //default password length $num=0; //default number of numbers
//Checks to see if password length was specified if(isset($_POST["pswd_length"]))
{
$len = (int) ($_POST["pswd_length"]);
} //Logic that determines what parameters to pass to gen_pass based on user input
if(isset($_POST["num"]))
{
$num = (int) ($_POST["num"]);
}
if((is_int($len) && $len > 0))
{
if(is_int($num) && $num <= $len)
{
$password = gen_pass($len, $num);
}
else if ($num > $len)
{
echo "Number of numbers is longer than password length. Using only password length.";
$password = gen_pass($len, NULL);
}
else
{
echo "Number of numbers is invalid. Using only password length.";
}
}
else
{
echo "Enter a valid password length.";
}
//Password generating gen_pass() function function gen_pass($length, $numb)
{
//Alphabet range (Lower and upper case)
$low_alpha = range('a','z');
$upp_alpha = range('A','Z');
//Randomly generated numbers
$nums = array();
//Location of random numbers (also randomly generated)
$placenums = array();
//Helper variable for placenums
$newrand=0;
$prevrand=-1;
if($numb!==NULL && ($GLOBALS['len'] >= $GLOBALS['num']))
{
//Generate random numbers and their random positions in password
for($i=0; $i<$numb; $i++)
{
$nums[$i] = mt_rand(0,9);
$newrand = mt_rand(0, $length-1);
//Makes sure $placenums doesn't contain duplicate values (=same position)
for($j=0; $j<count($placenums); $j++)
{
if($newrand==$placenums[$j])
{
$newrand = mt_rand(0, $length-1);
$j=-1;
} else { continue; }
}
$placenums[$i] = $newrand;
}
for($i=0; $i<$length; $i++)
{
/* Loop through $numb numbers to find a match
i.e. whether a $placenums location number matches the current $i
If so, add that number to the current location, else add letter */
for($j=0; $j<$numb; $j++)
{
if($i==$placenums[$j])
{
$GLOBALS['password'].=$nums[$j];
continue 2;
}
else
{
continue;
}
}
//Add random upper of lower case letter
$rnd = mt_rand(0,1);
if($rnd==0)
{
$GLOBALS['password'].=$upp_alpha[mt_rand(0,25)];
}
else
{
$GLOBALS['password'].=$low_alpha[mt_rand(0,25)];
}
}
}
else
{
for($i=0; $i<$length; $i++)
{
$rnd = mt_rand(0,2);
if($rnd==0)
{
$GLOBALS['password'].=mt_rand(0,9);
}
else if($rnd==1)
{
$GLOBALS['password'].=$low_alpha[mt_rand(0,25)];
}
else
{
$GLOBALS['password'].=$upp_alpha[mt_rand(0,25)];
}
}
}
return $GLOBALS['password'];
}
?>
<!-- PHP-Code End //-->
</head>
<body>
<h1>Simple Password Generator</h1>
<hr />
<form name="myForm" method="POST" action="<? echo $_SERVER['PHP_SELF']; ?>">
Password length:<input type="text" name="pswd_length" value="<? echo $len; ?>" /><br />
Number of numbers:<input type="text" name="num" value="<? echo $num; ?>" /><br />
Password:<input type="text" name="pwsd" value="<? echo $password; ?>" size="20" readonly /><br />
Password (lower-case):<input type="text" value="<? echo strtolower($password); ?>" size="20" readonly /><br />
<input type="submit" value="Generate" />
</form>
<hr />
</body>
</html>
|
|
|
Usage Example
|
|
|
Rate This Script
|
|
|
|