PHP implements a tool class that can accurately verify the ID number

Source: Internet
Author: User
This article mainly introduces the PHP implementation can accurately verify the identity card number of the tool class, involving PHP for string interception, calculation, judgment and other related operational skills, and accompanied by a description of the principle of the ID number, the need for friends can refer to the next

The example in this paper describes the tool class that PHP implements to accurately verify the ID number. Share to everyone for your reference, as follows:

<?phpclass Check_idcard {//$num as ID number, $checkSex: 1 for male, 2 for female, not for public function checkidentity ($num, $checkSex = ")  {//Not 15-bit or not 18-bit is an invalid ID number if (strlen ($num)! = && strlen ($num)! =) {return false;  }//is the value if (Is_numeric ($num)) {//If it is 15-bit ID number if (strlen ($num) = = 15) {//County (6-bit) $areaNum = substr ($num, 0,    6);    Date of birth (6 digits) $dateNum = substr ($num, 6, 6);   Gender (3-bit) $sexNum = substr ($num, 12, 3);    } else {//if it is a 18-digit ID number//County (6-bit) $areaNum = substr ($num, 0, 6);    Date of birth (8 digits) $dateNum = substr ($num, 6, 8);    Gender (3-bit) $sexNum = substr ($num, 14, 3);   Check code (1-bit) $endNum = substr ($num, 17, 1);   }} else {//is not a value if (strlen ($num) = =) {return false;    } else {//verify the first 17 bits are numeric, and 18 bits are characters x $check = substr ($num, 0, 17);    if (!is_numeric ($check)) {return false;    }//County (6-bit) $areaNum = substr ($num, 0, 6);    Date of birth (8 digits) $dateNum = substr ($num, 6, 8); Gender (3-bit) $sexNum = substr ($num, 14, 3);   Check code (1-bit) $endNum = substr ($num, 17, 1);    if ($endNum! = ' x ' && $endNum! = ' x ') {return false;   }}}//Verify the region if (Isset ($areaNum)) {if (! $this->checkarea ($areaNum)) {return false;   }}//Verify date if (Isset ($dateNum)) {if (! $this->checkdate ($dateNum)) {return false;     }}//Gender 1 is male, 2 is female if ($checkSex = = 1) {if (Isset ($sexNum)) {if (! $this->checksex ($sexNum)) {return false;    }}} elseif ($checkSex = = 2) {if (Isset ($sexNum)) {if ($this->checksex ($sexNum)) {return false;   }}}//Verify the last if (Isset ($endNum)) {if (! $this->checkend ($endNum, $num)) {return false; }} return true;  }//Verify the City Private function Checkarea ($area) {$num 1 = substr ($area, 0, 2);  $num 2 = substr ($area, 2, 2);  $num 3 = substr ($area, 4, 2);  According to Gb/t2260-999, the provinces and Cities Code 11 to (< $num 1 && $num 1 <) {return true;  } else {return false; }}//Verify the date of birth private function Checkdate ($date) {if (strlen ($date) = = 6) {$date 1 = substr ($date, 0, 2);   $date 2 = substr ($date, 2, 2);   $date 3 = substr ($date, 4, 2);  $statusY = $this->checky (' + '. $date 1);   } else {$date 1 = substr ($date, 0, 4);   $date 2 = substr ($date, 4, 2);   $date 3 = substr ($date, 6, 2);   $nowY = Date ("Y", Time ());   if (1900 < $date 1 && $date 1 <= $nowY) {$statusY = $this->checky ($date 1);   } else {return false; }} if (0 < $date 2 && $date 2 <) {if ($date 2 = = 2) {//Run year if ($statusY) {if (0 < $date     3 && $date 3 <=) {return true;     } else {return false;     }} else {//Common year if (0 < $date 3 && $date 3 <=) {return true;     } else {return false;    }}}} else {$maxDateNum = $this->getdatenum ($date 2);    if (0 < $date 3 && $date 3 <= $maxDateNum) {return true;    } else {return false;  }}} else {return false; }}//Verify gender private function CheckseX ($sex) {if ($sex% 2 = = 0) {return false;  } else {return true;  }}//verify 18-bit ID The last private function Checkend ($end, $num) {$checkHou = array (1, 0, ' X ', 9, 8, 7, 6, 5, 4, 3, 2);  $checkGu = Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);  $sum = 0;  for ($i = 0; $i < $i + +) {$sum + = (int) $checkGu [$i] * (int) $num [$i];  } $checkHouParameter = $sum% 11;  if ($checkHou [$checkHouParameter]! = $num [+]) {return false;  } else {return true;  }}//Verify common year Year, parameter year, return true to run year false for common year private Function Checky ($Y) {if (GetType ($Y) = = ' String ') {$Y = (int) $Y;   if ($Y% = = 0) {if ($Y% = = 0) {return true;   } else {return false;  }} else if ($Y% = = 0) {return true;  } else {return false; }}//month days parameter month (excluding February) returns the number of days Private function Getdatenum ($month) {if ($month = = 1 | | $month = = 3 | | $month = = 5 | | $mont H = = 7 | | $month = = 8 | | $month = = 10 | |  $month = =) {return 31; } else if ($month = = 2) {} else {return 30; }}}//Test Header ("Content-type:text/html;charset=utf-8"); $num = ' 230106199202099035 '; This number is randomly generated $test = new Check_idcard (), $data = $test->checkidentity ($num); Var_dump ($data);//============= New 18-digit ID number what you mean: =======================//1-2 Province, autonomous region, municipality code; 11-65//3-4 Prefecture-level city, AU, Autonomous Prefecture code;//5-6 Counties, county-level cities, district codes;//7-14 Birth date, For example, 19670401 for April 1, 1967,//15-17 is a sequence number, of which 17 men are singular, female is even;//18 bits are check codes, 0-9 and x, are randomly generated by the formula. Example://130503 19670401 0012 The meaning of this ID number: 13 for Hebei, 05 for Xingtai, 03 for Qiaoxi District, the date of birth is April 1, 1967, the order number is 001,2 for the verification code//===========15 ID number of people's meaning: ======================//1-2 Province, autonomous region, municipality code,//3-4-level city, AU, Autonomous Prefecture code,//5-6 County, city-level, district code;//7-12 Birth date, such as 670401 for April 1, 1967, This is the first difference from the 18-digit number; The//13-15 is a sequential number, of which 15 men are singular and the female is even;//the second difference from the 18-digit ID number: There is no last verification code. Example: The meaning of//130503 670401 001; 13 is Hebei, 05 is Xingtai, 03 is Qiaoxi District, date of birth is April 1, 1967, order number is 001.

Operation Result:

BOOL (TRUE)

The above is the whole content of this article, thank you for reading. Read more about topic.alibabacloud.com!

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.