Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6606247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:22:33+00:00 2026-05-25T19:22:33+00:00

Is this good enough for a random coupon code generator? Should I check and

  • 0

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:22:33+00:00Added an answer on May 25, 2026 at 7:22 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are emulators, but is this good enough? If someone is serious
is this good code? can it be simplified somehow? SELECT u.id,u.title,u.title,u.first,u.last FROM (((tblusers u
[Please edit the title if you find its not good enough] I have code
I need a 'good' way to initialize the pseudo-random number generator in C++. I've
Is this good enough ? do I need to add anything or remove anything
This is good: import string string.capwords("proper name") Out: 'Proper Name' This is not so
Following this question: Good crash reporting library in c# Is there any library like
Is this a good method to save to all index in a variable and
I'm wondering if this is good, bad? SelectRecipientResponse user = SomeUtil.SelectRecipient(p.Email, p.ListID.ToString()); bool userIsInList
Short version : echo testing | vim - | grep good This doesn't work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.