开发者

Random code generator for coupon system

Is this good enough for a random coupon code generator? Should I check and see if a code has already been used when I make a new code? What are the odds that this will repeat?

$coupon_code = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 7);

EDIT - here's my actual code:

$coupon_code = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 7);
$numrows = mysql_num_rows(mysql_query("SELECT id FROM generatedcoupons WHERE coupon_code='$coupon_code' LIMIT 1"));
if($numrows>0){
     $coupon_code =  substr(base_convert(sha1(uniqid(rand())), 16, 36), 0, 7);
     $numrows = mysql_num_rows(mysql_开发者_如何学运维query("SELECT id FROM generatedcoupons WHERE coupon_code='$coupon_code' LIMIT 1"));
     if($numrows>0)
          //error, show link to retry
}


Here's a coupon system that not only guarantees unique codes, but is very efficient when it comes to looking them up:

// assuming MySQL table with (id, code, effect)
mysql_query( "insert into `coupons` set `effect`='".$effect."'");
// "effect" will be some keyword to identify what the coupon does
$id = mysql_insert_id();
$code = $id."F";
$codelen = 32; // change as needed
for( $i=strlen($code); $i<$codelen; $i++) {
    $code .= dechex(rand(0,15));
}
mysql_query( "update `coupons` set `code`='".$code."' where `id`=".$id);

// now, when you are given a code to redeem, say $_POST['code']
list($code,$effect) = mysql_fetch_row( mysql_query( "select `code`, `effect` from `coupons` where `id`='".((int) $_POST['code'])."'"));
if( $code != $_POST['code']) die("Code not valid");
else {
    // do something based on $effect
}

As you can see, it takes the ID from AUTO_INCREMENT, appends an F, then pads with random hexadecimal characters. You can make $codelen as high as you want, but 32 should be plenty (giving around 16**26 combinations even after the millionth coupon).


Seems fairly random to me... I'd still probably double check that a code has not already been used when you create one on the very slight off chance that you do get a repetitive number.

And most likely the chances of a repetition are slightly more than 1 in 36^8 or roughly 1:2,821,109,907,456.

Note that since SHA1 gives back 40 digits in a hexadecimal string if you want there to be less chance that it will be duplicated you can always just increase your substr from (0,7) up to (0,29) (you can go even a little higher, but I doubt you want THAT big of a number)

But I defs agree you should have some sort of unique coupon id stored in some database for making sure that you don't have duplicates, and it would be a great way to see how many coupons you guys created (and potentially how many got used? =D)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜