好人寫的PHP版「身分證產生器」

Home Home
引用 | 編輯 我是好人
2008-07-15 17:45
樓主
推文 x0
有興趣的可以試試喔 ^^

來源: http://www.wretch.cc/blog/gump1002/7459167


複製程式
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
<title>身分證字號產生器</title>
</head>
<body>
<?php
class idCard_check{ //宣告一個類別,來送出身分證字號檢查碼
 var $id; //放檢查碼用的
 var $idx = array(1,1,1,1,1,1,1,1,3,1,1,2,2,2,3,2,2,2,2,2,2,2,3,3,3,3); //這兩個陣列,是用來把字母轉成數字用的
 var $idy = array(0,1,2,3,4,5,6,7,4,8,9,0,1,2,5,3,4,5,6,7,8,9,0,1,2,3); //ex. A=10, Z=33, I=34 ..etc
 function check($id_no){
  $id_no=ucfirst($id_no); //把字母轉成大寫
  for($i=0;$i<10;$i++) $ch[$i] = substr($id_no,$i,1); //把身分證字號轉成列陣
  $d=0; //判斷字母的位置,用來看$idx和$idy的對應值,例如Z的位置,在idx跟idy裡頭,就是3跟3 
  for($char="A";$char!=$ch[0];$char++){$d++;}//同上
  //下面那一行是用來實現身分證碼的演算法
  $id_num=$this->idx[$d]+$this->idy[$d]*9+$ch[1]*8+$ch[2]*7+$ch[3]*6+$ch[4]*5+$ch[5]*4+$ch[6]*3+$ch[7]*2+$ch[8]*1; 
  //下面是把數字除以10後求餘數
  $id=$id_num%10;
  //下面是把餘數用10減掉,求出檢查碼
  $id=10-$id;
  if($id==10){
   return 0;
  }else{ 
   return $id;
  } 
 } 
}
//下面兩行是設定亂數種子
mt_srand((double)microtime()*1000000);
srand((double)microtime()*1000000);
//
//下面是用來排列A~Z
$alpha_string=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$str="";
$str.=$alpha_string[array_rand($alpha_string,1)]; //亂數求出陣列的索引值
//
$str.=rand(1,2); //求男生,求女生(聽起來像拜註生娘娘....)
//
for($i=0;$i<8;$i++) $str.=mt_rand(0,9);//增加後面的八碼流水號
$tempCheck=new idCard_check;//宣告類別
$result=$tempCheck->check($str);//使用類別中的方法,回傳「正確的」餘數
$string.=substr_replace($str,$result,9);//把身分證碼最後一個字元取換成我們算出來的餘數
//這樣就有正確的身分證字號了
//
echo "<font color='red' size=ཆ'>".$string."</font>";
?>
<form name="idmaker" method="POST" action="idMaker.php">
<input type="submit" value='產生身分證字號'>
</form>
</body>
</html>


獻花 x0
引用 | 編輯 optometrist
2010-04-17 17:46
1樓
  
實用又有趣的程式碼

感謝分享阿 表情

獻花 x0
引用 | 編輯 trumanh14
2011-10-10 20:00
2樓
  
實用又有趣的程式碼
!

獻花 x0
引用 | 編輯 trumanh14
2011-10-10 20:01
3樓
  
name must be idMaker.php

獻花 x0