简体   繁体   中英

Create Coupon Code

I am developing a shopping site without the use of collective "solutions" ready. I want the client to receive the voucher in the mail from your account and the Panel of his account, he has a coupon available for printing. Each coupon that happens, you must have a unique code per customer. Does anyone have an idea how to do?

I use this code

while (true) {
    $ccode = "CP" . strtoupper ( dechex ( rand ( 1000000000, 9999999999) ) ) . strtoupper ( chr( rand( 65, 90 ) ) ) . chr(rand(65, 90));
    $check = mysql_query("SELECT coupon_code FROM item_coupons WHERE coupon_code = '$ccode'");
    if (mysql_num_rows($check) != 0) {
        //duplicate
    } else {
        break 1;
    }
}

A Hash of his email id can be used as coupon code. Also store it in database to avoid fraud.

使用PHP条形码制造商,那是我会做的

First of all you can create a coupon table in your database like this :

create table `coupon`(
    `code` char(32) primary key,
    `email` varchar(255),
    `used` tinyint(1) default 0
) ENGINE=MYISAM;

Now you can generate coupon completely random and then save it in the table for later checking :

function generate_coupon_for($email)
{
    do
    {
        $cpn = md5(rand());
        if(mysql_query("insert into `coupons`(`code`, `email`) values('$cpn', '$email');"))
        {
            return $cpn;
        }
    }while(mysql_errno() == 1062);
    //We had an error while trying to insert the coupon
}

And this way, you can check the coupon :

function check_coupon($code, $email)
{
    $q = mysql_query("update `coupons` set `used` = 1 where `email`='$email' and `code`='$code' and `used`=0;");
    return mysql_affected_rows() > 0;
}

It will return true or false...

I hope these can help you...

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