PHP sample code for verifying the validity of Chinese citizen ID card numbers
This article uses Java to verify the ID card numbers of Chinese citizens (15 or 18 digits). The functions are as follows:
- ID card number validity Verification
- Analyze detailed ID card information
- Generate a virtual province ID number.
ID card number verification
1. the number structure the citizenship number is a feature combination code, which consists of a 17-digit ontology code and a verification code. The six-digit address code, eight-digit birth date code, three-digit sequence code, and one-digit verification code are arranged from left to right.
2. Address Code (first six digits)
Indicates the Administrative Code of the county (city, flag, district) where the resident account of the encoding object is located, which is executed according to GB/T2260.
3. Date of Birth (seventh to fourteen digits)
Indicates the year, month, and day when the encoding object is born. The code is executed according to GB/T7408, and there is no separator between the year, month, and day codes.
4. sequence code (15th-17 digits)
Indicates the sequence number of the person born on the same year, the same month, and the same day within the region specified by the same address code. The odd number of the sequence code is allocated to men, and the even number is allocated to women.
5. Verification Code (18th digits)
(1) The weighted Sum formula S = Sum (Ai * Wi), I = 0 ,... , 16, first sum the right of the first 17 digits
Ai: indicates the number of ID card numbers at location I.
Wi: indicates the weighting factor at position I. Wi: 7 9 10 5 8 8 4 2 1 6 3 7 9 10 5 4 2
(2) computing model Y = mod (S, 11)
(3) obtain the corresponding verification code Y: 0 1 2 3 4 5 6 7 8 9 10 verification code: 1 0X9 8 7 6 5 4 3 2
IDValidator. php
<? Phpnamespace 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;} // query 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 digits without verification code, if ($ code ['type'] ===15) {self: $ cache [$ id] ['valid'] = true; return true ;} /* check bit * // position weighting $ posWeight = array (); for ($ I = 18; $ I> 1; $ I --) {$ wei = self :: $ util-> weight ($ I); $ posWeight [$ I] = $ wei ;} // Accumulate the weighted product of the body and position $ bodySum = 0; $ bodyArr = str_split ($ code ['body']); for ($ j = 0; $ j <count ($ bodyArr); $ j ++) {$ bodySum + = (intval ($ bodyArr [$ j], 10) * $ posWeight [18-$ j]);} // get the verification code $ checkBit = 12-($ bodySum % 11); if ($ checkBit = 10) {$ checkBit = 'X';} else if ($ checkBit> 10) {$ checkBit = $ checkBit % 11;} // check the verification code if ($ checkBit! = $ Code ['checkbit ']) {self: $ cache [$ id] ['valid'] = false; return false;} else {self :: $ cache [$ id] ['valid'] = true; return true ;}/// analyze detailed information function getInfo ($ id) {// The number must be valid if ($ this-> isValid ($ id) ===false) {return false;} // TODO reuse this part $ code = self :: $ util-> checkArg ($ id); // query the cache // by now, if (isset (self: $ cache [$ id]) has been recorded by isValid. & isset (self: $ cache [$ id] ['info']) {return s Elf: $ 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'];} // record cache self: $ cache [$ id] ['info'] = $ info; return $ info;} // create a function makeID ($ isFifteen = false) {// address code $ addr = null; if (self: $ GB2260! = Null) {$ loopCnt = 0; while ($ addr = null) {// prevents endless loops 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;} // 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 weighting $ posWeight = array (); for ($ I = 18; $ I> 1; $ I --) {$ wei = self :: $ util-> weight ($ I); $ posWeight [$ I] = $ wei;} // accumulate the weighted product of the body and position $ bodySum = 0; $ bodyArr = str_split ($ body); for ($ j = 0; $ j <count ($ bodyArr); $ j ++) {$ bodySum + = (intval ($ bodyArr [$ j], 10) * $ posWeight [18-$ j]);} // get the verification code $ checkBit = 12-($ bodySum % 11); if ($ checkBit = 10) {$ checkBit = 'X ';} else if ($ checkBit> 10) {$ checkBit = $ checkBit % 11;} return ($ body. $ checkBit );}}
Call
<? Phpheader ("Content-type: text/html; charset = UTF-8"); include 'idvalidator. php '; $ v = com \ jdk5 \ blog \ IDValidator: getInstance (); // generate an 18-digit id card number $ id = $ v-> makeID (); // get id card information $ info = $ v-> getInfo ($ id); var_dump ($ info ); // generate a 15-digit id card number $ id = $ v-> makeID (true); $ info = $ v-> getInfo ($ id); var_dump ($ info ); // verify that the ID card number is correct var_dump ($ v-> isValid ("123456789012345678 "));
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.