php編寫批量生成不重復(fù)的卡號(hào)密碼代碼
閑的時(shí)候,順便加強(qiáng)下自己對(duì)PHP中數(shù)組操縱的一些技巧,就寫了下面的一段小代碼,可以隨機(jī)生成卡號(hào)密碼對(duì)應(yīng)的數(shù)組,并且自動(dòng)去重復(fù),思路沒(méi)有,純粹瞎掰。
<?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']); //點(diǎn)數(shù)
if(intval($_POST['batch']>0)) $batch=intval($_POST['batch']); //批號(hào)
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萬(wàn)個(gè) %s點(diǎn) 的密碼</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組卡號(hào)密碼
$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);
?>
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
PHP5下$_SERVER變量不再受magic_quotes_gpc保護(hù)的彌補(bǔ)方法
在php5的環(huán)境中我們的$_SERVER變量將不再受magic_quotes_gpc的保護(hù),至于程序該如何加強(qiáng)自己的安全性,下面我們總結(jié)了怎么保護(hù)php中的cookie,get,post,files數(shù)據(jù)哦,有需要的朋友可參考一下2012-10-10
php curl中g(shù)zip的壓縮性能測(cè)試實(shí)例分析
這篇文章主要介紹了php curl中g(shù)zip的壓縮性能測(cè)試,結(jié)合實(shí)例形式分析了php使用curl的gzip壓縮耗時(shí)與效率,需要的朋友可以參考下2016-11-11
用PHP獲取Google AJAX Search API 數(shù)據(jù)的代碼
用PHP獲取Google AJAX Search API 數(shù)據(jù)的代碼2010-03-03
php session_start()出錯(cuò)原因分析及解決方法
本文是對(duì)php中session_start()的出錯(cuò)原因及解決方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10
使用php自動(dòng)備份數(shù)據(jù)庫(kù)表的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇使用php自動(dòng)備份數(shù)據(jù)庫(kù)表的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07

