Tag Archives: Random String

Generate a Random String, Random Password in MS Access

Building on an old post MS Access VBA – Generate a Random String, I wanted to post a fully functional sample in which I added full support for multiple string generation.  I use this tool to generate a set of random passwords and then pick one to create a new account password…

The beauty of this tool over many others, is that you run it locally, not on someone else’s server (god knows if your actions are being logged with malicious intent) like all the online generators do (yes, call me paranoid – in today’s world one cannot be too safe).  This way, you remain in control.

The other advantage is that the passwords are saved to a Table so you can continue to work with them easily. One word of caution however is that the table gets wiped clear every time you run the generator so be sure to output any result you wish to keep long term to another table or source (Excel, Word, …).

Random String Generator

Continue reading

PHP Random String Generator

Have you ever need to generate a random string or generate a unique id using PHP. Below is a complete example of how to do so. Simply copy the entire code into a new php page and then open it.

PHP Random String Generator

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>PHP Random String Generator</title>
</head>

<body>
    <?php
    # AUTHOR: 		Daniel Pineault, CARDA Consultants Inc.
    # DATE:			2010-06-25
    # PURPOSE: 		Generate a random string or unique Id
    # COPYRIGHT: 	You are free to use this code as you wish as long as
    #	 			this header is included in the final work.  This is 
    #				provided 'AS IS' and I assume no liability for its
    #				usage.
    #			

    if (empty($_POST)) {
        $UCase   = 1;
        $LCase   = 1;
        $Nbr     = 1;
        $SpecChrs = 1;
        $ExChrs  = "";
        $RandStrLen = 12;
    } else {
        foreach ($_POST as $key => $value) {
            $$key = strip_tags($value); //sanitize provided user entries
        }
        if (empty($UCase)) {
            $UCase = 0;
        }
        if (empty($LCase)) {
            $LCase = 0;
        }
        if (empty($Nbr)) {
            $Nbr = 0;
        }
        if (empty($SpecChrs)) {
            $SpecChrs = 0;
        }
        if (empty($ExChrs)) {
            $ExChrs = "";
        }
        if (empty($RandStrLen)) {
            $RandStrLen = 12;
        }
    }

    $sRandString = "";

    if (isset($GenRndStr)) {
        $s = "";
        $sUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $sLower = "abcdefghijklmnopqrstuvwxyz";
        $sNumbers = "0123456789";
        $sSpecial = "!@#$%^&*()[]{},.;:'/\~-+=|<>";

        if ($UCase == 1) {
            $s .= $sUpper;
        }
        if ($LCase == 1) {
            $s .= $sLower;
        }
        if ($Nbr == 1) {
            $s .= $sNumbers;
        }
        if ($SpecChrs == 1) {
            $s .= $sSpecial;
        }
        if (strlen($ExChrs) > 0) {
            for ($k = 0; $k <= (strlen($ExChrs) - 1); $k++) {
                $s = str_replace((string)$ExChrs[$k], "", $s);
            }
        }

        $sLen = $RandStrLen;

        for ($i = 1; $i <= $sLen; $i++) {
            $sRandString .= $s[Rand(1, strlen($s))];
        }
    }
    ?>
    <h1>PHP Random String Generator</h1>
    <form id="RndStrGen" name="RndStrGen" method="post">
        <table border="0" cellspacing="0" cellpadding="2">
            <tr>
                <td valign="top"><strong>Password Characteristics</strong> </td>
                <td><input type="checkbox" id="UCase" name="UCase" value="1" <?php
                                                                                if ($UCase == 1) {
                                                                                    echo ' checked="checked"';
                                                                                }
                                                                                ?> />
                    Upper Case Characters<br />
                    <input type="checkbox" id="LCase" name="LCase" value="1" <?php
                                                                                if ($LCase == 1) {
                                                                                    echo ' checked="checked"';
                                                                                }
                                                                                ?> />
                    Lower Case Characters<br />
                    <input type="checkbox" id="Nbr" name="Nbr" value="1" <?php
                                                                            if ($Nbr == 1) {
                                                                                echo ' checked="checked"';
                                                                            }
                                                                            ?> />
                    Numbers<br />
                    <input type="checkbox" id="SpecChrs" name="SpecChrs" value="1" <?php
                                                                                    if ($SpecChrs == 1) {
                                                                                        echo ' checked="checked"';
                                                                                    }
                                                                                    ?> />
                    Special Characters (!@#$%^&*()[]{},.;:'/\~-+=|<>)
                </td>
            </tr>
            <tr>
                <td valign="top"><strong>Characters to Exclude</strong></td>
                <td><input type="text" id="ExChrs" name="ExChrs" value="<?= $ExChrs ?>" /></td>
            </tr>
            <tr>
                <td><strong>Password Length</strong> </td>
                <td><input type="text" id="RandStrLen" name="RandStrLen" value="<?= $RandStrLen ?>" /></td>
            </tr>
        </table>
        <p>
            <input type="submit" id="GenRndStr" name="GenRndStr" value="Generate String" />
        </p>
    </form>
    <?php
    if (isset($sRandString)) {
        echo "<p>" . $sRandString . "</p>\n";
    }
    ?>
</body>

</html>