萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php編寫批量生成不重復的卡號密碼代碼

php編寫批量生成不重復的卡號密碼代碼

   本文給大家分享的是一個十分實用的代碼,項目中經常需要用到,可以批量生成不重復的卡號密碼的2種方法,有需要的小伙伴參考下吧。

  閒的蛋疼的時候,順便加強下自己對PHP中數組操縱的一些技巧,就寫了下面的一段小代碼,可以隨機生成卡號密碼對應的數組,並且自動去重復,思路沒有,純粹瞎掰。

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 <?php header('Content-Type:text/html; charset=utf-8'); function MakeCard() { set_time_limit(0);   //處理緩沖區 ob_end_clean(); ob_implicit_flush(true); echo str_pad(" ", 256);   if(intval($_POST['num']>0)) $num=intval($_POST['num']); //數量 if(intval($_POST['point']>0)) $point=intval($_POST['point']); //點數 if(intval($_POST['batch']>0)) $batch=intval($_POST['batch']); //批號 if(($_POST['ym']!="")) $ym=$_POST['ym']; //發行年月 else $ym=date('ym');   if($num==0) return;   $num=$num*100; //卡的張數,即記錄數   echo "<p>開始 ".date("H:i:s")." ";   for($i=1;$i<=$num;$i++) { $sn=sprintf("%02s%s%06s",$batch,$ym,$i); $seek=mt_rand(0,9999).mt_rand(0,9999).mt_rand(0,9999); //12位 $start=mt_rand(0,20); $str=strtoupper(substr(md5($seek),$start,12)); $str=str_replace("O",chr(mt_rand(65,78)),$str); $str=str_replace("0",chr(mt_rand(65,78)),$str); $row=array('sn'=>$sn,'password'=>$str,'created'=>time(),'point'=>$point); //查重 //在這裡加插入數據的代碼. print_r($row); } echo " 結束 ".date("H:i:s").""; printf("<br>成功生成:%s萬個 %s點 的密碼</p>",$num/1e4,$point); return $num; } //函數結束   $_POST['num']=1; $_POST['point']=10; $_POST['batch']=10; $_POST['ym']='1405'; echo MakeCard(); ?>

  方法二:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?php $numLen=16; $pwdLen=10; $c=100;//生成100組卡號密碼 $sNumArr=range(0,9); $sPwdArr=array_merge($sNumArr,range('A','Z'));   $cards=array(); for($x=0;$x< $c;$x++){ $tempNumStr=array(); for($i=0;$i< $numLen;$i++){ $tempNumStr[]=array_rand($sNumArr); } $tempPwdStr=array(); for($i=0;$i< $pwdLen;$i++){ $tempPwdStr[]=$sPwdArr[array_rand($sPwdArr)]; } $cards[$x]['no']=implode('',$tempNumStr); $cards[$x]['pwd']=implode('',$tempPwdStr); } array_unique($cards); print_r($cards); ?>

  以上所述就是本文的全部內容了,希望大家能夠喜歡。

copyright © 萬盛學電腦網 all rights reserved