// 009 Generate a near unique, non-sequential code
//
// Parameters: $codeSize - The size of the code to generate. Longer is more secure
// $codeChars - The set of characters to use to generate the key
//
// Returns: A simple string
//
function nearUniqueCode( $codeSize = 10, $codeChars= "" ) {
$code = "";
$chars = ($codeChars != "" ) ? $codeChars : "abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNPQRTUVWXYZ0123456789";
$chLen = strlen( $chars );
// Build the code
//
for ( $i=0; $i < $codeSize; $i++ ) {
$pos = mt_rand(0, $chLen-1);
$code .= substr( $chars, $pos, 1 );
}
return $code;
}
|