////
// Create a Coupon Code
function create_coupon_code($salt="secret", $length=SECURITY_CODE_LENGTH) {
global $db;
$good_result = 0;
$id1 = '';
$check = '731';
while ($good_result == 0) {
srand((double)microtime()*1000000); // seed the random number generator
// Generate the code (leave room for checksum!)
for($i = 0; $i < $length - 1; $i++) {
$id1 .= rand(0, 9);
}
// Add checksum =========================================
$checksum = 0;
for($i = 0; $i < $length - 1; $i++) {
$checksum = ($id1[$length - 1 - i] + 0) * ($check[$i%3] + 0);
}
$id1 .= strval(10 - ($checksum % 10));
// ==========================================
// Check that it's unique
$query = "select coupon_code from " . TABLE_COUPONS .
" where coupon_code = '" . $id1 . "'";
$rs = $db->Execute($query);
if ($rs->RecordCount() == 0) {
$good_result = 1;
} else {
$id1 = '';
}
}
return $id1;
}
BTW. The while-loop was not correct in the original code....
Bookmarks