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>