ID card checking principle and PHP implementation

Source: Internet
Author: User

Original address: http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21126994&id=3938244 on the problem of checking algorithm for ID cardIdentity card 15-bit upgrade to 18-bit, the original year with 2 bits and no last one, from left to right to represent ①1-2 upgrade Administrative Code ②3-4 level administrative Division code ③ 5-6 County Administrative Division code ④7-10 11-12 13-14 births, months, days ⑤ 15-17 sequential code, same region the same year , the same month, the same day the number of people born, odd is male, even is female ⑥18 check code, if it is 0-9 is 0-9, if 10 is represented by X (Roman numeral 10) ISO 7064:1983.mod 11-2 Checksum code∑ (A[i]*w[i]) MoD11(i =18,17,16 ... 2) The last one is the check digit 1

"*"Represents multiplication sign
I--------indicates the serial number of each digit of the ID number, from right to left, the leftmost side is 18, the rightmost is 1.
A[i]-----Represents the number of the ID number on the first digit
W[i]-----Represents the weighted value on the first bit of w[i] =2 ^(i-1) MoD11 "Get the sequence here is 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2"
Formula (1) to make the result R

According to the following table to find R corresponding check code is required for identification number of the check code C.
R01234567 8 9 10
C 1 0 X 9 8 7 6 5 4 3 2
So I see X is 10, and the Roman numeral is x .
1. Multiply the previous ID number 17 digits by the different coefficients respectively. The number of the first bit corresponds to [2^ (18-i)]mod11. The coefficients from the first to the 17th position are: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2;
2, add the result of multiplying the 17 digits and coefficients;
3, by adding out and dividing by 11, see the remainder is how much? ;
4, the remainder may have only 0 1 2 3 4 5 6 7 8 9 10 these 11 digits. The number of the last identity card corresponding to the respective is 1 0 X 9 8 7 6 5 4 3 2;
5, through the above know if the remainder is 2, will be in the ID card of the 18th digit of the Roman numeral Ⅹ. If the remainder is 10, the last number of the ID card is 2;

here is a simple PHP implementation that I wrote myself:

    <?php    /**    * Enter the ID number and verify the appropriate    **/    function Checkid ($id) {        $len = strlen ($id);        if ($len! =) {            return 0;        }        $a =str_split ($id, 1);        $w =array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);        $c =array (1,0, ' X ', 9,8,7,6,5,4,3,2);        $sum = 0;        for ($i =0; $i <17; $i + +) {                $sum = $sum + $a [$i]* $w [$i];        }        $r = $sum%11;        $res = $c [$r];        The echo "Check digit is:". $res;        if ($res = = $a []) {            return 1;        } else {            return 0;        }    }

Use Excel to validate the model, assuming that the number is inside the cell A1, then enter the following formula in the calculation column to calculate:


    1. =lookup (MOD (MID ($A $1,1,1) *mod (2^17,11) +mid ($A $1,2,1) *mod (2^16,11) +mid ($A $1,3,1) *mod (2^15,11) +mid ($A $1,4,1) *MOD (2^14,11) +mid ($A $1,5,1) *mod (2^13,11) +mid ($A $1,6,1) *mod (2^12,11) +mid ($A $1,7,1) *mod (2^11,11) +mid ($A $1,8,1) *MOD (2 ^10,11) +mid ($A $1,9,1) *mod (2^9,11) +mid ($A $1,10,1) *mod (2^8,11) +mid ($A $1,11,1) *mod (2^7,11) +mid ($A $1,12,1) *MOD (2^ 6,11) +mid ($A $1,13,1) *mod (2^5,11) +mid ($A $1,14,1) *mod (2^4,11) +mid ($A $1,15,1) *mod (2^3,11) +mid ($A $1,16,1) *MOD (2^ 2,11) +mid ($A $1,17,1) *mod (2^1,11), 11), {0;1;2;3;4;5;6;7;8;9;10},{1;0; " X "; 9;8;7;6;5;4;3;2})
The following figure is a model for verifying the identity card:



here are the GB documents issued by the countrygb11643-1999 National standard citizen identification number. pdf

ID card checking principle and PHP implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.