This article mainly introduces PHP implementation of Chinese citizen ID card number Validation example code, can determine the correctness of the ID card number, very practical value
This article will use Java to achieve the Chinese citizen (15-bit or 18-bit) identification number of the relevant authentication, the function is as follows:
Identification Number validity verification
Analyze Detailed ID Information
Generate a virtual province certificate number.
Identification Number Verification
1, the structure of the number of citizen identification number is a feature combination code, consisting of 17-bit digital body code and a check code. The sorting order is from left to right: six-digit address code, eight-digit birth date code, three-digit sequential code, and one-digit check code.
2. Address code (first six digits)
The Code of administrative divisions in the county (city, Flag, district) of the permanent residence of the coded object shall be executed according to gb/t2260.
3. Birth date code (seventh to 14 digits)
Represents the year, month, and day of the birth of the encoded object, executed according to gb/t7408, with no separator between the year, month, and day code.
4, Sequential code (15th to 17)
Indicates the sequence number of the same year, the same month, and the same date as the area identified by the same address code, and the odd numbers of sequential codes are assigned to males, even to females.
5. Check code (18th digit)
(1) Weighted summation formula for 17-bit digital body code S = SUM (Ai * Wi), i = 0, ..., 16, sum of first 17 digits
Ai: A numeric value representing the ID number at position i
Wi: Indicates the weighting factor at position I wi:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
(2) calculate modulo Y = mod (S, 11)
(3) The corresponding check code is obtained by means of y:0 1 2 3 4 5 6 7 8 9 10 Check code: 1 0 X 9 8 7 6 5 4 3 2
idvalidator.php
<?phpnamespace Com\jdk5\blog\idvalidator; Class Idvalidator {private static $GB 2260, private static $instance, private static $cache = Array (), private static $uti L 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:: $GB 2260 = 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;}//Query the 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;} The 15-bit does not contain a checksum to this end if ($code [' type '] = = =) {self:: $cache [$id] [' valid '] = true; return true;} /* Check digit part *//Position weighted $posWeight = array (); for ($i =; $i > 1; $i-) {$wei = self:: $util->weight ($i); $posWeight [$i] = $wei;} Accumulate body part and position weighted product $bodySum = 0; $BODYARR = Str_split ($code [' body ']); for ($j = 0; $j < count ($BODYARR), $j + +) {$bodySum + = (intval ($bodyArr [$j], Ten) * $posWeight [18-$j]);} The verification Code $checkBit =-($bodySum% 11); if ($checkBit = =) {$checkBit = ' X ';} else if ($checkBit >) {$checkBit = $checkBit% 11;}//Check the checksum if ($checkB It! = $code [' checkbit ']) {self:: $cache [$id] [' valid '] = False, return false,} else {self:: $cache [$id] [' valid '] = TR Uereturn true; }}//parse details function GetInfo ($id) {///number must be valid if ($this->isvalid ($id) = = = False) {return false;}//TODO reuse this section $co De = self:: $util->checkarg ($id); Query cache//By this time the cache record is already available through IsValid (Isset (self:: $cache [$id]) && isset (self:: $cache [$id] [' info ']) { Return self:: $cache [$id] [' info ']; } $addr = substr ($code [' body '], 0, 6); $birth = ($code [' type '] = = = = substr ($code [' body '], 6, 8): substr ($code [' body '], 6, 6)); $order = substr ($code [' body '],-3); $info = Array (); $info [' addrcode '] = $addr; if (self:: $GB 2260!== null) {$info [' addr '] = self:: $util->getaddrinfo ($ADDR);} $info [' Birth '] = ($code [' type '] = = = 18? (substr ($birth, 0, 4). '-' . SUBSTR ($birth, 4, 2). '-' . SUBSTR ($birth,-2)): (' + '. 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 '] = = =) {$info [' checkbit '] = $code [' Checkbit '];} RememberRecord cache self:: $cache [$id] [' info '] = $info; return $info; }//Imitation of a number function Makeid ($isFifteen =false) {//address code $ADDR = NULL, if (self:: $GB 2260!== null) {$loopCnt = 0; while ($ addr = = = NULL) {//Prevent Dead loop if ($loopCnt >) {$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:: $GB 2260[$addrTest])) {$addr = $addrTest; Break } $loopCnt + +; }} else {$addr = 110101;} Year of birth $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 '); Position weighted $posWeight = array (); for ($i =; $i > 1; $i-) {$wei = self:: $util->weight ($i); $posWeight [$i] = $wei;} Accumulate body part and position weighted product $bodySum = 0; $BODYARR = Str_split ($body); for ($j = 0; $j < count ($BODYARR), $j + +) {$bodySum + = (intval ($bodyArr [$j], Ten) * $posWeight [18-$j]);} The verification Code $checkBit =-($bodySum% 11); if ($checkBit = =) {$checkBit = ' X ';} else if ($checkBit >) {$checkBit = $checkBit%;} return ($body. $che Ckbit); }}
Call
<?phpheader ("content-type:text/html; Charset=utf-8 "), include ' idvalidator.php '; $v = Com\jdk5\blog\idvalidator\idvalidator::getinstance ();//Generate a 18-bit ID number $id = $v->makeid ();//obtain identity card information $info = $v->getinfo ($id); Var_dump ($info);//Generate a 15-bit ID number $id = $v->makeid (true); info = $v->getinfo ($id), Var_dump ($info),//Verify that the ID number is correct var_dump ($v->isvalid ("123456789012345678"));
The above is the whole content of this article, I hope that everyone's study has helped.