header image

password

have you seen this?

Apple
Apple "Get a Mac" Ad: Surgery


have you played this?

Flight of the Hamsters-thumbnail
Flight of the Hamsters



free money is always good, too...

need a quick, random password?

well, here's a script i wrote that generates a random password with a given number of characters, with the choice to select Upper case, Lower case, numberes and / or special characters.

try it out below, and if you like it, keep it. the source code is available to cut/paste below. all i ask is that you somehow link back to this page (or anywhere else on this site). enjoy!


number of characters (8+ for stronger passwords, 999 limit)
Use upper case characters (ABCD...)
Use lower case characters (abcd...)
Use numbers (0123...)
Use special characters (!@#$...)


===========================================================

source code:
<?php
ob_start
(); //start gathering data. prevents buffer overflow

function fGenerateRandomPassword($length,$useUpperCase,$useLowerCase,$useNumbers,$useSpecialChars)
    {
    
//define strings for each category
    
$upperCase "ABCDEFGHIJKLNOPQRSTUVWXYZ";
    
$lowerCase strtolower($upperCase);
    
$numbers "0123456789";
    
$specialChars "!@#$%^&*()?";

    
//adds characters from category if selected
    
$toUse "";
    
$countToUse 0;
    if(
$useUpperCase == "TRUE") { $toUse .= $upperCase$countToUse++; }
    if(
$useLowerCase == "TRUE") { $toUse .= $lowerCase$countToUse++; }
    if(
$useNumbers == "TRUE") { $toUse .= $numbers$countToUse++; }
    if(
$useSpecialChars == "TRUE") { $toUse .= $specialChars;  $countToUse++; }
    
    if(
preg_replace('/^\d+$/','',$length)==$length// check that only numbers are entered if javascript is disabled
        
{
        return 
"<b>Please enter only numbers in the field.</b>\n";
        }
    if(
$length $countToUse//if number entered is less that the count of selected character sets.
        
{
        return 
"<b>\"number of characters\" can not be less than selection of characters to use.</b>\n";
        }

    
//no errors, generate the password
    
$password "";
    for (
$i 0$i $length$i++)
        {
        
$password .= $toUse[(rand() % strlen($toUse))];
        }

    
//define the array to return
    
$passwordArray[0] = $password;
    
$passwordArray[1] = $upperCase;
    
$passwordArray[2] = $lowerCase;
    
$passwordArray[3] = $numbers;
    
$passwordArray[4] = $specialChars;

    
//check that the password contains at LEAST 1 character from each selected category
    
$hasUpper strpbrk($passwordArray[0],$passwordArray[1]);
    
$hasLower strpbrk($passwordArray[0],$passwordArray[2]);
    
$hasNumber strpbrk($passwordArray[0],$passwordArray[3]);
    
$hasSpecChar strpbrk($passwordArray[0],$passwordArray[4]);
    if((
$useUpperCase == "TRUE" && $hasUpper == "") || ($useLowerCase == "TRUE" && $hasLower == "") || ($useNumbers == "TRUE" && $hasNumber == "") || ($useSpecialChars == "TRUE" && $hasSpecChar == ""))
        {
        return 
"FALSE";
        }

    
//it contains everything it needs... return the password
    
else
        {
        
$password "Your password is: <b>".$passwordArray[0]."</b>\n";
        return 
$password;
        }
    }

if(isset(
$_POST["generatePassword"]))
    {
    
$length $_POST['length'];
    
$countToUse 0;
    
$useUpper $_POST['useUpper'];
    if(
$useUpper == "on"$useUpper "TRUE";
    
$useLower $_POST['useLower'];
    if(
$useLower == "on"$useLower "TRUE";
    
$useNum $_POST['useNumbers'];
    if(
$useNum == "on"$useNum "TRUE";
    
$useSpec $_POST['useSpecialChars'];
    if(
$useSpec == "on"$useSpec "TRUE";

    
//make a password, and if it does not contain at least 1 character from each category, get a new password until it does.
    
do
        {
        
$password fGenerateRandomPassword($length,$useUpper,$useLower,$useNum,$useSpec);
        }
    while(
$password == "FALSE");

    
//display password
    
echo $password;
    echo 
"<br /><br />\n------------------------------------------<br /><br />\n";
    }

//display form for options. 'onkeyup' erases any non-numeric characters.
?>
<form name="passwordGenerator" id="passwordGenerator" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
<tr><td><input type="text" name="length" id="length" value="8" size="2" maxlength="3" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')"/></td><td>number of characters (8+ for stronger passwords, 999 limit)</td></tr>
<tr><td align="right"><input type="checkbox" name="useUpper" id="useUpper" checked/></td><td>Use upper case characters (ABCD...)</td></tr>
<tr><td align="right"><input type="checkbox" name="useLower" id="useLower" checked/></td><td>Use lower case characters (abcd...)</td></tr>
<tr><td align="right"><input type="checkbox" name="useNumbers" id="useNumbers" checked/></td><td>Use numbers (0123...)</td></tr>
<tr><td align="right"><input type="checkbox" name="useSpecialChars" id="useSpecialChars" checked/></td><td>Use special characters (!@#$...)</td></tr>
<tr><td align="middle" colspan="2"><input type="submit" name="generatePassword" id="generatePassword" value="generate password"/>
</table>
</form>

<?php
ob_end_flush
(); //done gathering, let it all out!
?>
1