PHP產生隨機不重復激活碼


 1     /**
 2      * 生成激活碼
 3      */
 4     public function create_cdk()
 5     {
 6         if (IS_POST) {
 7             $cdk_num = intval(I('post.cdk_num'));  //生成數量
 8             if ($cdk_num) {
 9                 for ($i = 0; $i < $cdk_num; $i++) {
10                     //查詢是否重復
11                     //$cdkey=create_randomstr_s();
12                     $check = true;
13                     while ($check==true) {
14                         $cdkey = create_randomstr_s();
15                         $result = M('course_cdkey')->where(array('cdkey' => $cdkey))->find();
16                         if (empty($result)) {
17                             $data['cdkey'] = $cdkey;
18                             $check = false;
19                         }
20                     }
21                     $data['validity']=I('post.validity');
22       
23                     $data['addTime']=time();
24                     $result=M('course_cdkey')->add($data);
25                 }
26                 if($result){
27                     $this->success('操作成功');
28                 }
29             }
30         } else {
31             $this->display('create_cdk');
32         }
33 
34     }

 1 /**
 2  * 生成隨機字符串(數字字母小寫)
 3  * @param string $lenth 長度
 4  * @return string 字符串
 5  */
 6 function create_randomstr_s($lenth = 6) {
 7 
 8     return random($lenth, '123456789abcdefghijklmnpqrstuvwxyz');
 9 
10 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM