溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP如何實(shí)現(xiàn)中國(guó)公民身份證號(hào)碼有效性驗(yàn)證

發(fā)布時(shí)間:2021-06-22 14:39:07 來(lái)源:億速云 閱讀:434 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)PHP如何實(shí)現(xiàn)中國(guó)公民身份證號(hào)碼有效性驗(yàn)證,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

功能如下:

  1. 身份證號(hào)有效性驗(yàn)證

  2. 分析詳細(xì)身份證信息

  3. 生成一個(gè)虛擬的省份證號(hào)碼。

身份證號(hào)碼驗(yàn)證

1、號(hào)碼的結(jié)構(gòu) 公民身份號(hào)碼是特征組合碼,由十七位數(shù)字本體碼和一位校驗(yàn)碼組成。排列順序從左至右依次為:六位數(shù)字地址碼,八位數(shù)字出生日期碼,三位數(shù)字順序碼和一位數(shù)字校驗(yàn)碼。

2、地址碼(前六位數(shù))

表示編碼對(duì)象常住戶口所在縣(市、旗、區(qū))的行政區(qū)劃代碼,按GB/T2260的規(guī)定執(zhí)行。

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

表示編碼對(duì)象出生的年、月、日,按GB/T7408的規(guī)定執(zhí)行,年、月、日代碼之間不用分隔符。

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

表示在同一地址碼所標(biāo)識(shí)的區(qū)域范圍內(nèi),對(duì)同年、同月、同日出生的人編定的順序號(hào), 順序碼的奇數(shù)分配給男性,偶數(shù)分配給女性。

5、校驗(yàn)碼(第十八位數(shù))

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

Ai:表示第i位置上的身份證號(hào)碼數(shù)字值

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

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

(3)通過(guò)模得到對(duì)應(yīng)的校驗(yàn)碼 Y: 0 1 2 3 4 5 6 7 8 9 10 校驗(yàn)碼: 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 'GB2260.php';
 }
 if (!class_exists("com\jdk5\blog\IDValidator\util")){
 include 'util.php';
 }
 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] ['valid'] !== false) {
 return self::$cache [$id] ['valid'];
 } else {
 if (! isset ( self::$cache [ $id ] )) {
 self::$cache [$id] = array ();
 }
 }
 
 $addr = substr ( $code ['body'], 0, 6 );
 $birth = $code ['type'] === 18 ? substr ( $code ['body'], 6, 8 ) :
 substr ( $code ['body'], 6, 6 );
 $order = substr ( $code ['body'], - 3 );
 
 if (! (self::$util->checkAddr ( $addr ) && self::$util->checkBirth ( $birth ) &&
 self::$util->checkOrder ( $order ))) {
 self::$cache [$id] ['valid'] = false;
 return false;
 }
 
 // 15位不含校驗(yàn)碼,到此已結(jié)束
 if ($code ['type'] === 15) {
 self::$cache [$id] ['valid'] = true;
 return true;
 }
 
 /* 校驗(yàn)位部分 */
 
 // 位置加權(quán)
 $posWeight = array ();
 for($i = 18; $i > 1; $i --) {
 $wei = self::$util->weight ( $i );
 $posWeight [$i] = $wei;
 }
 
 // 累加body部分與位置加權(quán)的積
 $bodySum = 0;
 $bodyArr = str_split( $code ['body'] );
 for($j = 0; $j < count ( $bodyArr ); $j ++) {
 $bodySum += (intval ( $bodyArr [$j], 10 ) * $posWeight [18 - $j]);
 }
 
 // 得出校驗(yàn)碼
 $checkBit = 12 - ($bodySum % 11);
 if ($checkBit == 10) {
 $checkBit = 'X';
 } else if ($checkBit > 10) {
 $checkBit = $checkBit % 11;
 }
 // 檢查校驗(yàn)碼
 if ($checkBit != $code ['checkBit']) {
 self::$cache [$id] ['valid'] = false;
 return false;
 } else {
 self::$cache [$id] ['valid'] = true;
 return true;
 }
 }
 // 分析詳細(xì)信息
 function getInfo ($id) {
 // 號(hào)碼必須有效
 if ($this->isValid($id) === false) {
 return false;
 }
 // TODO 復(fù)用此部分
 $code = self::$util->checkArg($id);
 
 // 查詢cache
 // 到此時(shí)通過(guò)isValid已經(jīng)有了cache記錄
 if (isset(self::$cache[$id]) && isset(self::$cache[$id]['info'])) {
 return self::$cache[$id]['info'];
 }
 
 $addr = substr($code['body'], 0, 6);
 $birth = ($code['type'] === 18 ? substr($code['body'], 6, 8) :
 substr($code['body'], 6, 6));
 $order = substr($code['body'], -3);
 
 $info = array();
 $info['addrCode'] = $addr;
 if (self::$GB2260 !== null) {
 $info['addr'] = self::$util->getAddrInfo($addr);
 }
 $info ['birth'] = ($code ['type'] === 18 ? (substr ( $birth, 0, 4 ) . '-' . substr ( $birth, 4, 2 ) . '-' . substr ( $birth, - 2 )) : ('19' . substr ( $birth, 0, 2 ) . '-' . substr ( $birth, 2, 2 ) . '-' . substr ( $birth, - 2 )));
 $info['sex'] = ($order % 2 === 0 ? 0 : 1);
 $info['length'] = $code['type'];
 if ($code['type'] === 18) {
 $info['checkBit'] = $code['checkBit'];
 }
 
 // 記錄cache
 self::$cache[$id]['info'] = $info;
 
 return $info;
 }
 
 // 仿造一個(gè)號(hào)
 function makeID ($isFifteen=false) {
 // 地址碼
 $addr = null;
 if (self::$GB2260 !== null) {
 $loopCnt = 0;
 while ($addr === null) {
 // 防止死循環(huán)
 if ($loopCnt > 50) {
  $addr = 110101;
  break;
 }
 $prov = self::$util->str_pad(self::$util->rand(66), 2, '0');
 $city = self::$util->str_pad(self::$util->rand(20), 2, '0');
 $area = self::$util->str_pad(self::$util->rand(20), 2, '0');
 $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, '0');
 $mo = self::$util->str_pad(self::$util->rand(12, 1), 2, '0');
 $da = self::$util->str_pad(self::$util->rand(28, 1), 2, '0');
 if ($isFifteen) {
 return $addr . $yr . $mo . $da
 . self::$util->str_pad(self::$util->rand(999, 1), 3, '1');
 }
 
 $yr = '19' . $yr;
 $body = $addr . $yr . $mo . $da . self::$util->str_pad(self::$util->rand(999, 1), 3, '1');
 
 // 位置加權(quán)
 $posWeight = array();
 for ($i = 18; $i > 1; $i--) {
 $wei = self::$util->weight($i);
 $posWeight[$i] = $wei;
 }
 
 // 累加body部分與位置加權(quán)的積
 $bodySum = 0;
 $bodyArr = str_split($body);
 for ($j = 0; $j < count($bodyArr); $j++) {
 $bodySum += (intval($bodyArr[$j], 10) * $posWeight[18 - $j]);
 }
 
 // 得出校驗(yàn)碼
 $checkBit = 12 - ($bodySum % 11);
 if ($checkBit == 10) {
 $checkBit = 'X';
 } else if ($checkBit > 10) {
 $checkBit = $checkBit % 11;
 }
 return ($body . $checkBit);
 }
}

調(diào)用

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

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

//生成一個(gè)18位身份證號(hào)
$id = $v->makeID();
//獲取身份證信息
$info = $v->getInfo($id);
var_dump($info);
//生成一個(gè)15位身份證號(hào)
$id = $v->makeID(true);
$info = $v->getInfo($id);
var_dump($info);

//驗(yàn)證身份證號(hào)是否正確
var_dump($v->isValid("123456789012345678"));

關(guān)于“PHP如何實(shí)現(xiàn)中國(guó)公民身份證號(hào)碼有效性驗(yàn)證”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI