首頁  >  文章  >  後端開發  >  PHP實作中國公民身分證號碼有效性驗證的方法

PHP實作中國公民身分證號碼有效性驗證的方法

墨辰丷
墨辰丷原創
2018-05-23 10:56:392037瀏覽

這篇文章主要介紹了PHP實作中國公民身分證號碼有效性驗證範例程式碼,可以判斷身分證號碼的正確性,非常具有實用價值

本文將使用Java實現中國公民(15位元或18位元)身分證號碼的相關驗證,功能如下:

  1. 身分證號有效性驗證

  2. 分析詳細身分證資訊

  3. 產生一個虛擬的省證號碼。

身分證號碼驗證

#1、號碼的結構公民身分號碼是特徵組合碼,由十七位數本體碼和一位校驗碼組成。排列順序由左至右依序為:六位數字位址碼,八位數字出生日期碼,三位數字順序碼及一位數字校驗碼。

2、地址碼(前六位數)

表示編碼物件常住戶口所在縣(市、旗、區)的行政區劃代碼,依GB/ T2260的規定執行。

3、出生日期碼(第七位至十四位)

表示編碼物件出生的年、月、日,依GB/T7408的規定執行,年、月、日代碼之間不用分隔符號。

4、順序碼(第十五位至十七位元)

表示在同一位址碼所識別的區域範圍內,對同年、同月、同日出生的人編定的順序號, 順序碼的奇數分配給男性,偶數分配給女性。

5、校驗碼(第十八位數)

(1)十七位數字本體碼加權求和公式S = Sum(Ai * Wi ), i = 0, … , 16 ,先對前17位數字的權求和

Ai:表示第i位置上的身分證號碼數字值

#Wi:表示第i位置上的加權因子Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2

#(2)計算模Y = mod(S, 11)

(3)透過模得到對應的校驗碼Y: 0 1 2 3 4 5 6 7 8 9 10 校驗碼: 1 0 X 9 8 7 6 5 4 3 2

IDValidator.php

<?php
namespace com\jdk5\blog\IDValidator;
 
class IDValidator {
 private static $GB2260;
 private static $instance;
 private static $cache = array();
 private static $util;
 function __construct() {
 if (!class_exists("com\jdk5\blog\IDValidator\GB2260")){
 include &#39;GB2260.php&#39;;
 }
 if (!class_exists("com\jdk5\blog\IDValidator\util")){
 include &#39;util.php&#39;;
 }
 self::$GB2260 = GB2260::getGB2260 ();
 self::$util = util::getInstance();
 }
 public static function getInstance() {
 if (is_null ( self::$instance )) {
 self::$instance = new IDValidator ();
 }
 return self::$instance;
 }
 function isValid($id) {
 $code = self::$util->checkArg ( $id );
 if ($code === false) {
 return false;
 }
 // 查询cache
 if (isset ( self::$cache [ $id ] ) && self::$cache [$id] [&#39;valid&#39;] !== false) {
 return self::$cache [$id] [&#39;valid&#39;];
 } else {
 if (! isset ( self::$cache [ $id ] )) {
 self::$cache [$id] = array ();
 }
 }
 
 $addr = substr ( $code [&#39;body&#39;], 0, 6 );
 $birth = $code [&#39;type&#39;] === 18 ? substr ( $code [&#39;body&#39;], 6, 8 ) :
 substr ( $code [&#39;body&#39;], 6, 6 );
 $order = substr ( $code [&#39;body&#39;], - 3 );
 
 if (! (self::$util->checkAddr ( $addr ) && self::$util->checkBirth ( $birth ) &&
 self::$util->checkOrder ( $order ))) {
 self::$cache [$id] [&#39;valid&#39;] = false;
 return false;
 }
 
 // 15位不含校验码,到此已结束
 if ($code [&#39;type&#39;] === 15) {
 self::$cache [$id] [&#39;valid&#39;] = true;
 return true;
 }
 
 /* 校验位部分 */
 
 // 位置加权
 $posWeight = array ();
 for($i = 18; $i > 1; $i --) {
 $wei = self::$util->weight ( $i );
 $posWeight [$i] = $wei;
 }
 
 // 累加body部分与位置加权的积
 $bodySum = 0;
 $bodyArr = str_split( $code [&#39;body&#39;] );
 for($j = 0; $j < count ( $bodyArr ); $j ++) {
 $bodySum += (intval ( $bodyArr [$j], 10 ) * $posWeight [18 - $j]);
 }
 
 // 得出校验码
 $checkBit = 12 - ($bodySum % 11);
 if ($checkBit == 10) {
 $checkBit = &#39;X&#39;;
 } else if ($checkBit > 10) {
 $checkBit = $checkBit % 11;
 }
 // 检查校验码
 if ($checkBit != $code [&#39;checkBit&#39;]) {
 self::$cache [$id] [&#39;valid&#39;] = false;
 return false;
 } else {
 self::$cache [$id] [&#39;valid&#39;] = true;
 return true;
 }
 }
 // 分析详细信息
 function getInfo ($id) {
 // 号码必须有效
 if ($this->isValid($id) === false) {
 return false;
 }
 // TODO 复用此部分
 $code = self::$util->checkArg($id);
 
 // 查询cache
 // 到此时通过isValid已经有了cache记录
 if (isset(self::$cache[$id]) && isset(self::$cache[$id][&#39;info&#39;])) {
 return self::$cache[$id][&#39;info&#39;];
 }
 
 $addr = substr($code[&#39;body&#39;], 0, 6);
 $birth = ($code[&#39;type&#39;] === 18 ? substr($code[&#39;body&#39;], 6, 8) :
 substr($code[&#39;body&#39;], 6, 6));
 $order = substr($code[&#39;body&#39;], -3);
 
 $info = array();
 $info[&#39;addrCode&#39;] = $addr;
 if (self::$GB2260 !== null) {
 $info[&#39;addr&#39;] = self::$util->getAddrInfo($addr);
 }
 $info [&#39;birth&#39;] = ($code [&#39;type&#39;] === 18 ? (substr ( $birth, 0, 4 ) . &#39;-&#39; . substr ( $birth, 4, 2 ) . &#39;-&#39; . substr ( $birth, - 2 )) : (&#39;19&#39; . substr ( $birth, 0, 2 ) . &#39;-&#39; . substr ( $birth, 2, 2 ) . &#39;-&#39; . substr ( $birth, - 2 )));
 $info[&#39;sex&#39;] = ($order % 2 === 0 ? 0 : 1);
 $info[&#39;length&#39;] = $code[&#39;type&#39;];
 if ($code[&#39;type&#39;] === 18) {
 $info[&#39;checkBit&#39;] = $code[&#39;checkBit&#39;];
 }
 
 // 记录cache
 self::$cache[$id][&#39;info&#39;] = $info;
 
 return $info;
 }
 
 // 仿造一个号
 function makeID ($isFifteen=false) {
 // 地址码
 $addr = null;
 if (self::$GB2260 !== null) {
 $loopCnt = 0;
 while ($addr === null) {
 // 防止死循环
 if ($loopCnt > 50) {
  $addr = 110101;
  break;
 }
 $prov = self::$util->str_pad(self::$util->rand(66), 2, &#39;0&#39;);
 $city = self::$util->str_pad(self::$util->rand(20), 2, &#39;0&#39;);
 $area = self::$util->str_pad(self::$util->rand(20), 2, &#39;0&#39;);
 $addrTest = $prov . $city . $area;
 if (isset(self::$GB2260[$addrTest])) {
  $addr = $addrTest;
  break;
 }
 $loopCnt ++;
 }
 } else {
 $addr = 110101;
 }
 
 // 出生年
 $yr = self::$util->str_pad(self::$util->rand(99, 50), 2, &#39;0&#39;);
 $mo = self::$util->str_pad(self::$util->rand(12, 1), 2, &#39;0&#39;);
 $da = self::$util->str_pad(self::$util->rand(28, 1), 2, &#39;0&#39;);
 if ($isFifteen) {
 return $addr . $yr . $mo . $da
 . self::$util->str_pad(self::$util->rand(999, 1), 3, &#39;1&#39;);
 }
 
 $yr = &#39;19&#39; . $yr;
 $body = $addr . $yr . $mo . $da . self::$util->str_pad(self::$util->rand(999, 1), 3, &#39;1&#39;);
 
 // 位置加权
 $posWeight = array();
 for ($i = 18; $i > 1; $i--) {
 $wei = self::$util->weight($i);
 $posWeight[$i] = $wei;
 }
 
 // 累加body部分与位置加权的积
 $bodySum = 0;
 $bodyArr = str_split($body);
 for ($j = 0; $j < count($bodyArr); $j++) {
 $bodySum += (intval($bodyArr[$j], 10) * $posWeight[18 - $j]);
 }
 
 // 得出校验码
 $checkBit = 12 - ($bodySum % 11);
 if ($checkBit == 10) {
 $checkBit = &#39;X&#39;;
 } else if ($checkBit > 10) {
 $checkBit = $checkBit % 11;
 }
 return ($body . $checkBit);
 }
}

呼叫

##

<?php
header("Content-type: text/html; charset=utf-8");

include &#39;IDValidator.php&#39;;
$v = com\jdk5\blog\IDValidator\IDValidator::getInstance();

//生成一个18位身份证号
$id = $v->makeID();
//获取身份证信息
$info = $v->getInfo($id);
var_dump($info);
//生成一个15位身份证号
$id = $v->makeID(true);
$info = $v->getInfo($id);
var_dump($info);

//验证身份证号是否正确
var_dump($v->isValid("123456789012345678"));

#以上就是本文的全部內容,希望對大家的學習有所幫助。


相關推薦:

php簡單實作短網址(短鏈)還原的方法(測試可用)_php技巧

PHP的PSR規格中文版_php基礎

#測試PHP連結MYSQL成功與否的程式碼_php基礎

#

以上是PHP實作中國公民身分證號碼有效性驗證的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn