One way to make a random password generator. See it in action: Random Password Generator Example
Hopefully I’ve commented enough in the code to explain it all:
<?php
/**
* generateRandomPasswords.php
* @author Robert Newton <lektrikpukeAtYahooDotCom>
* if you use/reuse my code, please give a mention to my site/blog--Thanks!
*/
/** arrays where characters are stored
* note: special characters like & (ampersand) and # (pound) can cause display issues
*/
$capLetters = 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');
$lowercase = 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');
$numbers = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
$specials = array('!', '@', '#', '$', '%', '^', '*', '+', '=', '_', '~', '{', '}');
// default counts of characters, overridden when correct values are entered into form
$lowerCount = 2;
$numCount = 3;
$capCount = 1;
$specialCount = 3;
// change this to change how many times the password is (re)shuffled and displayed
$shuffleCount = 4;
// don't change below
$password = '';
$eMess = '';
/**
* genRandom
* loops through passed in array $arr $cCount number of times picking characters
* randomly and adding to return string $randchars
* @param array $arr
* @param int $cCount
* @return string
*/
function genRandom($arr, $cCount) {
$arrCount = count($arr) - 1; // counts chars in array--used to set upper limit of random
$randchars = '';
for ($i = 0; $i < $cCount; $i++) {
$randchars .= $arr[mt_rand(0, $arrCount)];
}
return $randchars;
}
if (isset($_POST['submit'])) {
try {
foreach ($_POST as $key => $val) {
if ($val !== 'Generate' && $val !== '') { // exclude submit button and empty values
if (!is_numeric($val) || abs((int)$val) > 99) { // if it's not a number(int) or greater than 99, throw an error
throw new Exception('Value must be numeric: ' . $val);
} else { // else assign value to variable (overriding default)
${$key} = abs((int)$val); // negatives are not allowed
}
}
}
} catch (Exception $e) { // if an error occurred, catch it and add to error message
$eMess .= $e->getMessage();
}
}
// call function passing in array and number of characters wanted (to be generated)
$password .= genRandom($lowercase, $lowerCount);
$password .= genRandom($numbers, $numCount);
$password .= genRandom($capLetters, $capCount);
$password .= genRandom($specials, $specialCount);
// shuffle generated password $shuffleCount number of times
for ($i = 0; $i < $shuffleCount; $i++) {
echo "<br />" . strlen($password) . ' char shuffled: ' . str_shuffle($password);
}
echo "<br /><br />Contains " . $lowerCount . ' lowercase, ' . $capCount
. ' uppercase, ' . $specialCount . ' special chars, and ' . $numCount . ' numbers!';
?>
<br /><br />
<form name="generate" method="post" action="genRandom.php">
<label>Lower case: <input type="text" name="lowerCount" /></label>
<label style="margin-left: 15px;">Upper case: <input type="text" name="capCount" /></label>
<label style="margin-left: 15px;">Special: <input type="text" name="specialCount" /></label>
<label style="margin-left: 15px;">Number: <input type="text" name="numCount" /></label>
<input style="margin-left: 15px;" type="submit" name="submit" value="Generate" />
</form>
< ?php
// if $eMess is not blank/empty, display it
if ($eMess != '')
echo $eMess;
?>
