


PHP method to implement validity verification of Chinese citizen ID number
This article mainly introduces PHP to implement the sample code for validating the Chinese citizen ID number, which can determine the correctness of the ID number and is of great practical value.
This article will use Java to implement the Chinese citizen (15 or 18 digits) ID number, the functions are as follows:
ID card number validity verification
Analyze detailed ID card information
Generate a virtual province certificate number.
ID card number verification
1. Number structure The citizen identity number is a characteristic combination code, consisting of a seventeen-digit body code and a check code. The order from left to right is: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit check code.
2. Address code (first six digits)
represents the administrative division code of the county (city, banner, district) where the coding object’s permanent residence is located, in GB/ The provisions of T2260 are implemented.
3. Date of birth code (seventh to fourteenth digit)
indicates the year, month and day of birth of the coding object, and shall be implemented in accordance with the provisions of GB/T7408 , no separators are used between year, month, and day codes.
4. Sequence code (15th to 17th digit)
means that within the area identified by the same address code, the same year, the same month, the same People born on the same day are assigned sequential numbers, with odd numbers assigned to men and even numbers assigned to women.
5. Check code (18th digit)
(1) Weighted summation formula of seventeen-digit ontology code S = Sum(Ai * Wi ), i = 0, …, 16, first sum the weights of the first 17 digits
Ai: represents the digital value of the ID number at the i-th position
Wi: represents the weighting factor Wi at the i-th position: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
(2) Calculate modulus Y = mod(S, 11)
(3) Get the corresponding check code Y through the module: 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
<?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位不含校验码,到此已结束 if ($code ['type'] === 15) { self::$cache [$id] ['valid'] = 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 ['body'] ); for($j = 0; $j < count ( $bodyArr ); $j ++) { $bodySum += (intval ( $bodyArr [$j], 10 ) * $posWeight [18 - $j]); } // 得出校验码 $checkBit = 12 - ($bodySum % 11); if ($checkBit == 10) { $checkBit = 'X'; } else if ($checkBit > 10) { $checkBit = $checkBit % 11; } // 检查校验码 if ($checkBit != $code ['checkBit']) { self::$cache [$id] ['valid'] = false; return false; } else { self::$cache [$id] ['valid'] = 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]['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; } // 仿造一个号 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, '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'); // 位置加权 $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 = 'X'; } else if ($checkBit > 10) { $checkBit = $checkBit % 11; } return ($body . $checkBit); } }
Call
<?php header("Content-type: text/html; charset=utf-8"); include 'IDValidator.php'; $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"));
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
php simple method to restore short URLs (short links) (available for testing)_php tips
PHP Chinese version of PSR specification_php basics
TestPHPConnect to MYSQL Code for success or failure_php basics
The above is the detailed content of PHP method to implement validity verification of Chinese citizen ID number. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
