PHP: Password generator from simple to complex, free code

Password generation can be a hassle at time. Being human, we like to take the easy way out and stay with the known. However, when it comes to passwords you need to venture into the unknown. These are some neat, little scripts to help you generate random passwords.

Simply copy and paste the code into a file with a “.php” extension. Encase the code in an open and closed PHP environment code ?>To run the file type: “php file.php” return. If you have PHP installed on your computer is will generate the passwords.

This first snippet is the simplest generator. By setting the password length variable you will obtain a random set of numbers for a password to that length.

$passlength="8";

while ($passlength){
$mystring = mt_rand(0,9);
$mypasswd = $mypasswd . $mystring;
$passlength--;
}
echo $mypasswd . "
";

This snippet is for generating random letters, both upper and lower case.

$passlength="8";
while ($passlength){
$letter_array = array("a","b","c","d","e","f","g", "h",
"i","j","k","l","m","n","o","p","q","r","s","t"
,"u","v","w","x","y","z","A","B","C","D","E","F","G",
"H","I","J","K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z");
$myletterstring = $letter_array[mt_rand(0,51)];
$mypassword = $mypassword . $myletterstring;
$passlength--;
}
echo $mypassword . "
";

This snippet is to generate upper and lower case letters and numbers. Since the array item number is randomly generated, you can mix the array any way you want. Remember, the key here is password length. The longer the stronger.

$passlength="8";
while ($passlength){
$letter_number_array = array("a","b","c","d","e","f","g",
 "h","i","j","k","l","m","n","o","p","q","r","s","t","u",
"v","w","x","y","z","A","B","C","D","E","F","G","H","I",
"J","K","L","M","N","O","P","Q","R","S","T","U","V","W",
"X","Y","Z", "0","1","2","3","4","5","6","7","8","9");
$myletterandnumberstring = $letter_number_array[mt_rand(0,61)];
$mypassword1 = $mypassword1 . $myletterandnumberstring;
$passlength--;
}
echo $mypassword1 . " 
";

Here is the final way to generate a random password using the PHP shuffle function.

$passlength="8";
while ($passlength){
$numbers = array("a","b","c","d","e","f","g", "h","i","j","k","l","m","n","o","p","q","r","s","t","u",
"v","w","x","y","z","A","B","C","D","E","F","G","H","I",
"J","K","L","M","N","O","P","Q","R","S","T","U","V","W",
"X","Y","Z", "0","1","2","3","4","5","6","7","8","9");
shuffle($numbers);
$myshuffle = $numbers[$passlength];
$mynewpassword = $mynewpassword . $myshuffle;
$passlength--;
}
echo $mynewpassword . "
";

Remember, when dealing with an array the first item is 0. That means you need to subtract 1 from the numerical item count. You can also add symbols to the array; however, many applications will not take symbols due to the password encryption method.

To see how random the numbers generated are, copy and paste all three snippets in the same PHP file.

Hope this helps and have fun making your passwords harder to guess.

Comments are closed.