PHP generates five-digit invitation code

Source: Internet
Author: User
Tags recode
This article to everyone to share the content is PHP generated five-digit invitation code, has a certain reference value, the need for friends can refer to

Model

/**     * Generate invitation code     * @param $uid     * @return string     */public function Initcode () {        $code = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ ';        $rand = $code [rand (0,25)]            . Strtoupper (Dechex (date (' M '))).            Date (' d '). substr (Time (), -5)            . substr ( Microtime (), 2,5)            . sprintf ('%02d ', rand (0,99));        for (            $a = MD5 ($rand, true),            $s = ' 0123456789ABCDEFGHIJKLMNOPQRSTUV ',            $d = ',            $f = 0;            $f < 5;            $g = Ord ($a [$f]),            $d. = $s [($g ^ ord ($a [$f + 8]))-$g & 0x1F],            $f + +        );        return $d;    }    /**     * Whether to repeat */public    function Recode ($code) {        if ($this->where (' code= '. $code)->find ()) {            return false;        }        return true;    }


Action

/** * My invitation code * @author Zhangfeng */Public Function Mycode () {$model = M (' Usercode ')        );        $uid = ($_get[' uid ');        $code = $model->is_code ($uid);        $this->assign (' Code ', $code);            if (! $code) {$code = $this->getcode ($model, $uid);            $res = $model->savecode ($uid, $code);            if ($res) {$this->assign (' Code ', $code); } else {$this->assign (' Code '), ' Invitation code generation failed!            , error: '. $model->geterror ());    }} $this->display ();    }/** * Recursive function gets an absolutely non-repeating invitation code * @author Zhangfeng * @param $model * @param $uid * @return Mixed * *        Private Function GetCode ($model, $uid) {$code = $model->initcode ($uid);        if ($model->recode ($code)) {return $code;            } else {while (true) {$this->getcode ($model, $uid); }        }    }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.