title: Random code generator for coupon system
body: <p>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?</p>
  
  <pre><code>$coupon_code = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 7);
  </code></pre>
  
  <p>EDIT - here's my actual code:</p>
  
  <pre><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&gt;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&gt;0)
            //error, show link to retry
  }
  </code></pre>