简体   繁体   中英

Generate & Expire coupon-codes/promo-codes

We are making a lottery website. In which we want to generate some coupon-codes/promo-codes. Is there any tried and tested code written which generates coupon-codes and expires them after exact 24hrs? We are using (PHP/mySQL).

Thanks in advance.

There is. It's called time() and is inside every single PHP distribution.

To expire a token, you need 2 steps:

  • On creating the coupon you call time() which generates an integer and add the number of seconds you want the token to be valid to it, then store the result in an unsigned int field called expires
  • On checking a token you compare this stored value with the current output of time() - if the latter is bigger the coupon has expired.

You can clean up expired tokens with DELETE FROM tablename WHERE expires<UNIX_TIMESTAMP() , but I would not recommend going this route naively, as it will rob you the possibility to differentiate between an expired and an inexistant token.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM