{"id":192,"date":"2014-04-18T11:56:16","date_gmt":"2014-04-18T15:56:16","guid":{"rendered":"http:\/\/www.lektrikpuke.com\/blog\/?p=192"},"modified":"2014-04-19T10:51:06","modified_gmt":"2014-04-19T14:51:06","slug":"random-password-generator","status":"publish","type":"post","link":"https:\/\/lektrikpuke.com\/blog\/2014\/04\/18\/random-password-generator\/","title":{"rendered":"Random Password Generator"},"content":{"rendered":"<p>One way to make a random password generator.  See it in action: <a href=\"http:\/\/www.lektrikpuke.com\/additional-pages\/generateRandomPasswords.php\" target=\"_blank\">Random Password Generator Example<\/a><\/p>\n<p>Hopefully I&#8217;ve commented enough in the code to explain it all:<\/p>\n<pre style=\"font-size:12px; line-height:1.1em; letter-spacing:.002em; word-spacing:-.2em;\">\r\n&lt;?php\r\n\/**\r\n * generateRandomPasswords.php\r\n * @author Robert Newton &lt;lektrikpukeAtYahooDotCom&gt;\r\n * if you use\/reuse my code, please give a mention to my site\/blog--Thanks!\r\n *\/\r\n\r\n\/** arrays where characters are stored\r\n * note:  special characters like & (ampersand) and # (pound) can cause display issues\r\n *\/\r\n$capLetters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'\r\n    , 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');\r\n$lowercase = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'\r\n    , 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');\r\n$numbers = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');\r\n$specials = array('!', '@', '#', '$', '%', '^', '*', '+', '=', '_', '~', '{', '}');\r\n\r\n\/\/ default counts of characters, overridden when correct values are entered into form\r\n$lowerCount = 2;\r\n$numCount = 3;\r\n$capCount = 1;\r\n$specialCount = 3;\r\n\r\n\/\/ change this to change how many times the password is (re)shuffled and displayed\r\n$shuffleCount = 4;\r\n\r\n\/\/ don't change below\r\n$password = '';\r\n$eMess = '';\r\n\r\n\/**\r\n * genRandom\r\n * loops through passed in array $arr $cCount number of times picking characters\r\n * randomly and adding to return string $randchars\r\n * @param array $arr\r\n * @param int $cCount\r\n * @return string\r\n *\/\r\nfunction genRandom($arr, $cCount) {\r\n    $arrCount = count($arr) - 1; \/\/ counts chars in array--used to set upper limit of random\r\n    $randchars = '';\r\n    for ($i = 0; $i &lt; $cCount; $i++) {\r\n        $randchars .= $arr[mt_rand(0, $arrCount)];\r\n    }\r\n    return $randchars;\r\n}\r\n\r\nif (isset($_POST['submit'])) {\r\n    try {\r\n        foreach ($_POST as $key =&gt; $val) {\r\n            if ($val !== 'Generate' && $val !== '') { \/\/ exclude submit button and empty values\r\n                if (!is_numeric($val) || abs((int)$val) > 99) { \/\/ if it's not a number(int) or greater than 99, throw an error\r\n                    throw new Exception('Value must be numeric: ' . $val);\r\n                } else { \/\/ else assign value to variable (overriding default)\r\n                    ${$key} = abs((int)$val); \/\/ negatives are not allowed\r\n                }\r\n            }\r\n        }\r\n    } catch (Exception $e) { \/\/ if an error occurred, catch it and add to error message\r\n        $eMess .= $e-&gt;getMessage();\r\n    }\r\n}\r\n\r\n\/\/ call function passing in array and number of characters wanted (to be generated)\r\n$password .= genRandom($lowercase, $lowerCount);\r\n$password .= genRandom($numbers, $numCount);\r\n$password .= genRandom($capLetters, $capCount);\r\n$password .= genRandom($specials, $specialCount);\r\n\r\n\/\/ shuffle generated password $shuffleCount number of times\r\nfor ($i = 0; $i &lt; $shuffleCount; $i++) {\r\n    echo \"&lt;br \/&gt;\" . strlen($password) . ' char shuffled: ' . str_shuffle($password);\r\n}\r\necho \"&lt;br \/&gt;&lt;br \/&gt;Contains \" . $lowerCount . ' lowercase, ' . $capCount \r\n        . ' uppercase, ' . $specialCount . ' special chars, and ' . $numCount . ' numbers!';\r\n?&gt;\r\n&lt;br \/&gt;&lt;br \/&gt;\r\n&lt;form name=\"generate\" method=\"post\" action=\"genRandom.php\"&gt;\r\n    &lt;label&gt;Lower case: &lt;input type=\"text\" name=\"lowerCount\" \/&gt;&lt;\/label&gt;\r\n    &lt;label style=\"margin-left: 15px;\"&gt;Upper case: &lt;input type=\"text\" name=\"capCount\" \/&gt;&lt;\/label&gt;\r\n    &lt;label style=\"margin-left: 15px;\"&gt;Special: &lt;input type=\"text\" name=\"specialCount\" \/&gt;&lt;\/label&gt;\r\n    &lt;label style=\"margin-left: 15px;\"&gt;Number: &lt;input type=\"text\" name=\"numCount\" \/&gt;&lt;\/label&gt;\r\n    &lt;input style=\"margin-left: 15px;\" type=\"submit\" name=\"submit\" value=\"Generate\" \/&gt;\r\n&lt;\/form&gt;\r\n\r\n&lt; ?php\r\n\/\/ if $eMess is not blank\/empty, display it\r\nif ($eMess != '')\r\n    echo $eMess;\r\n?&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>One way to make a random password generator. See it in action: Random Password Generator Example Hopefully I&#8217;ve commented enough in the code to explain it all: &lt;?php \/** * generateRandomPasswords.php * @author Robert Newton &lt;lektrikpukeAtYahooDotCom&gt; * if you use\/reuse &hellip; <a href=\"https:\/\/lektrikpuke.com\/blog\/2014\/04\/18\/random-password-generator\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72,149],"tags":[6,89],"class_list":["post-192","post","type-post","status-publish","format-standard","hentry","category-computer-stuff","category-php-computer-stuff","tag-php","tag-script"],"_links":{"self":[{"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/posts\/192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/comments?post=192"}],"version-history":[{"count":15,"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":207,"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/posts\/192\/revisions\/207"}],"wp:attachment":[{"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/media?parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/categories?post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/tags?post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}