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 }
原文地址:https://www.cnblogs.com/wang-yang/p/7426784.html