php編寫批量生成不重復(fù)的卡號密碼代碼
更新時間:2024年07月02日 11:42:07 投稿:hebedich
本文給大家分享的是一個十分實用的代碼,項目中經(jīng)常需要用到,可以批量生成不重復(fù)的卡號密碼的2種方法,有需要的小伙伴參考下吧。
閑的時候,順便加強下自己對PHP中數(shù)組操縱的一些技巧,就寫了下面的一段小代碼,可以隨機生成卡號密碼對應(yīng)的數(shù)組,并且自動去重復(fù),思路沒有,純粹瞎掰。
<?php header('Content-Type:text/html; charset=utf-8'); function MakeCard() { set_time_limit(0); //處理緩沖區(qū) ob_end_clean(); ob_implicit_flush(true); echo str_pad(" ", 256); if(intval($_POST['num']>0)) $num=intval($_POST['num']); //數(shù)量 if(intval($_POST['point']>0)) $point=intval($_POST['point']); //點數(shù) if(intval($_POST['batch']>0)) $batch=intval($_POST['batch']); //批號 if(($_POST['ym']!="")) $ym=$_POST['ym']; //發(fā)行年月 else $ym=date('ym'); if($num==0) return; $num=$num*100; //卡的張數(shù),即記錄數(shù) 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); //查重 //在這里加插入數(shù)據(jù)的代碼. print_r($row); } echo " 結(jié)束 ".date("H:i:s").""; printf("<br>成功生成:%s萬個 %s點 的密碼</p>",$num/1e4,$point); return $num; } //函數(shù)結(jié)束 $_POST['num']=1; $_POST['point']=10; $_POST['batch']=10; $_POST['ym']='1405'; echo MakeCard(); ?>
方法二:
<?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); ?>
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
防止用戶利用PHP代碼DOS造成用光網(wǎng)絡(luò)帶寬
一打開IIS,服務(wù)器的流出帶寬就用光-----就是說服務(wù)器不斷向別人發(fā)包,這個情況和受到DDOS攻擊是不同的,DDOS是不斷收到大量數(shù)據(jù)包.2011-03-03探討:如何使用PHP實現(xiàn)計算兩個日期間隔的年、月、周、日數(shù)
本篇文章是對使用PHP實現(xiàn)計算兩個日期間隔的年、月、周、日數(shù)的方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06刪除數(shù)組元素實用的PHP數(shù)組函數(shù)
php之從數(shù)組中刪除空白的元素(包括只有空白字符的元素)將一個二維數(shù)組轉(zhuǎn)換為 hashmap2008-08-08