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); } } }