php 生成8位數惟一的激活碼

/**
*生成激活碼
*/
function showGenerationActivationCode(){
#渠道類型id
$channel_id=$_POST['channel'];
#根據渠道id去查詢渠道英文名稱
$channelInfo = load_mysql ( "channelInfo" );
$_res=$channelInfo->getInfoById($channel_id);
$en_name=$_res['en_name'];
#活動類型
$type=$_POST['active_type'];
#生成數量
$nub=$_POST['nub'];
#鏈接redis
$redis=new Redis();
$redis->connect('192.168.1.133',8899);
#受權
$redis->auth("XXXXXX");
#存入數據
for($i=0;$i<$nub;$i++){
#接受生成的激活碼
$ActivationCode=$this->showunique_rand(10000000,99999999,1);
#生成code
$code=$this->showAlgorithm_ActivationCode($type,$channel_id,$ActivationCode);
#寫入redis
$redis->hmset('hash','code_hash',array('code'=>$code));
}
#寫入redis
$redis->hmset('hash', 'channel_hash', array($en_name=> $channel_id ));php

$this->PromptMsg = "生成成功!";
$this->UrlJump = "./index.php?module=operation&action=ActivationCode&menuId=168";
$this->promptMsg ();
}
/**
*生成code算法
*type:活動類型[取前三位]
*channel_id:渠道id[取前三位,不足以0填充]
*array_ActivationCode:激活碼數組
*code算法格式=活動類型+渠道id+array_ActivationCode;
*return code的數組
*/
function showAlgorithm_ActivationCode($type,$channel_id,$ActivationCode){
#截取活動類型前位字符串
$type=mb_substr($type,0,3,'utf-8');
#渠道id[取前三位,不足左邊以0填充]
$channel_id=str_pad($channel_id,3,'0',STR_PAD_LEFT);
#拼接code
$code=$type.''.$channel_id.''.$ActivationCode;
return $code;
}
/**
*生成8位數的激活碼算法
*/
function showunique_rand($min, $max, $num=1) {
$count = 0;
$return = array();
while ($count < $num) {
$return[] = mt_rand($min, $max);
$return = array_flip(array_flip($return));
$count = count($return);
}
return $return[0];
}mysql

相關文章
相關標籤/搜索