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

$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];
}

转载于:https://www.cnblogs.com/hgj123/p/4531799.html

你可能感兴趣的:(php 生成8位数唯一的激活码)