//Verify that the ID is validfunctionValidateidcard ($IDCard) {if(strlen ($IDCard) = = -) {return check18idcard ($IDCard); } ElseIf ((strlen ($IDCard)== the) {$IDCard=convertidcard15to18 ($IDCard); Return Check18idcard ($IDCard); } Else{returnfalse; }}//calculate the last verification code of the ID card, according to national standard GB 11643-1999functionCalcidcardcode ($IDCardBody) {if(Strlen ($IDCardBody)! = -) {returnfalse; } //weighting Factor$factor = Array (7,9,Ten,5,8,4,2,1,6,3,7,9,Ten,5,8,4,2); //check code corresponding value$code = Array ('1','0','X','9','8','7','6','5','4','3','2'); $checksum=0; for($i =0; $i < strlen ($IDCardBody); $i + +) {$checksum+ = substr ($IDCardBody, $i,1) *$factor [$i]; } return $code [$checksum% One];}//Upgrade 15-bit ID to 18-bitfunctionconvertidcard15to18 ($IDCard) {if(Strlen ($IDCard)! = the) {returnfalse; } Else { //if the ID sequence code is 996 997 998 999, these are special codes for centenarians older than if(Array_search (substr ($IDCard, A,3), Array ('996','997','998','999')) !==false) {$IDCard= substr ($IDCard,0,6) .' -'. SUBSTR ($IDCard,6,9); } Else{$IDCard= substr ($IDCard,0,6) .' +'. SUBSTR ($IDCard,6,9); }} $IDCard=$IDCard. Calcidcardcode ($IDCard); return $IDCard;}//Verification Code validity check for 18-bit ID cardfunctionCheck18idcard ($IDCard) {if(Strlen ($IDCard)! = -) {returnfalse; } $IDCardBody= substr ($IDCard,0, -);//identity Card Subject$IDCardCode = Strtoupper (substr ($IDCard, -,1));//ID Card The last Verification code if(Calcidcardcode ($IDCardBody)! =$IDCardCode) {returnfalse; } Else{returntrue; }}
Method of Use Validateidcard (' ID card ');
/** * According to the identity card, whether the age condition is met * @param type $IDCard ID * @param type $minAge minimum age*/functionIsmeetagebyidcard ($IDCard, $minAge) {$ret=Validateidcard ($IDCard); if($ret = = =FALSE) {return FALSE; } if(Strlen ($IDCard) <= the) {$IDCard=convertidcard15to18 ($IDCard); } $year=Date('Y')-substr ($IDCard,6,4); $monthDay=Date('MD')-substr ($IDCard,Ten,4); Return ($year> $minAge | | $year = = $minAge && $monthDay >0) ?True:false;}
Structure of the ID card number
Identity card number is a feature combination code, consisting of 17-bit digital body code and a check code.
Order from left to right according to this: six-digit address code, eight-digit birth date code, three-bit digital sequence code and a digit check code.
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.
Date-of-Birth code (seventh to 14)
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.
Sequential code (15th to 17 bits)
Indicates the range of the area identified by the same address code, the sequence number of the same year, the same month, the same day birth, the sequence code is assigned to men, odd numbers are assigned to women.
Check code (18th digit number)
1. Weighted summation formula for 17-bit digital body code
s= sum (Ai * Wi), i=0, ..., 16, the first 17 digits of the right sum.
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. Calculation mode
Y = mod (S, 11)
3. Get the corresponding check code by the module
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
To verify the ID number method:
<?PHPfunctionCheckidcard ($idcard) {//can only be 18-bit if(Strlen ($idcard)! = -) {returnfalse; } //Remove the Body code$idcard _base = substr ($idcard,0, -); //Remove Check Code$verify _code = substr ($idcard, -,1); //weighting Factor$factor = Array (7,9,Ten,5,8,4,2,1,6,3,7,9,Ten,5,8,4,2); //check code corresponding value$verify _code_list = Array ('1','0','X','9','8','7','6','5','4','3','2'); //check code based on the first 17 bits$total =0; for($i =0; $i < -; $i + +) {$total+ = substr ($idcard _base, $i,1)*$factor [$i]; } //Take the mold$mod = $total% One; //Compare Check Codes if($verify _code = =$verify _code_list[$mod]) {returntrue; }Else{returnfalse; }} $idcard='fill in the ID number to be verified here'; Var_dump (Checkidcard ($idcard));?>
PHP Verified ID Valid according to national standard GB 11643-1999 15-bit and 18-bit universal