PHP随机生成不重复的8位卡号(数字)和卡密(字符串)-php教程

资源魔 32 0
1、天生没有反复的随机数字,可自界说长度

/**
 * 天生没有反复的随机数字
 * @param  int $start  需求天生的数字开端范畴
 * @param  int $end    完结范畴
 * @param  int $length 需求天生的随机数个数
 * @return number      天生的随机数
 */
function getRandNumber($start=0,$end=9,$length=8){
//初始化变量为0
$connt = 0;
//建一个新数组
$temp = array();
while($connt < $length){
//正在肯定范畴内随机天生一个数放入数组中
$temp[] = mt_rand($start, $end);
//$data = array_unique($temp);
//去除了数组中的反复值用了“翻翻法”,就是用array_flip()把数组的key以及value替换两次。这类做法比用 array_unique() 快患上多。
$data = array_flip(array_flip($temp));
//将数组的数目存入变量count中
$connt = count($data);
}
//为数组付与新的键名
shuffle($data);
//数组转字符串
$str=implode(",", $data);
//交换掉逗号
$number=str_replace(',', '', $str);
return $number;
}

2、随机天生没有反复的8位卡密

function makeCardPassword() {
        $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 < 8;
            $g = ord( $a[ $f ] ),
            $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
            $f++
        );
        return  $d;
}

相干保举:《PHP教程》

以上就是PHP随机天生没有反复的8位卡号(数字)以及卡密(字符串)的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学

抱歉,评论功能暂时关闭!