Php ID card verification

Source: Internet
Author: User
Tags strlen
The code is as follows: Copy code
<? Php
Class IdcardAction extends Action {
// PHP automatically obtains the corresponding Constellation function based on the ID card number
Function get_xingzuo ($ cid) {// automatically returns the corresponding Constellation based on the ID card number
If (! $ This-> isIdCard ($ cid) return '';
$ Bir = substr ($ cid, 10, 4 );
$ Month = (int) substr ($ bir, 0, 2 );
$ Day = (int) substr ($ bir, 2 );
$ StrValue = '';
If ($ month = 1 & $ day> = 20) | ($ month = 2 & $ day <= 18 )){
$ StrValue = "Aquarius ";
} Else if ($ month = 2 & $ day> = 19) | ($ month = 3 & $ day <= 20 )){
$ StrValue = "Pisces ";
} Else if ($ month = 3 & $ day> 20) | ($ month = 4 & $ day <= 19 )){
$ StrValue = "Aries ";
} Else if ($ month = 4 & $ day> = 20) | ($ month = 5 & $ day <= 20 )){
$ StrValue = "Taurus ";
} Else if ($ month = 5 & $ day> = 21) | ($ month = 6 & $ day <= 21 )){
$ StrValue = "Gemini ";
} Else if ($ month = 6 & $ day> 21) | ($ month = 7 & $ day <= 22 )){
$ StrValue = "cancer ";
} Else if ($ month = 7 & $ day> 22) | ($ month = 8 & $ day <= 22 )){
$ StrValue = "Leo ";
} Else if ($ month = 8 & $ day> = 23) | ($ month = 9 & $ day <= 22 )){
$ StrValue = "Virgo ";
} Else if ($ month = 9 & $ day> = 23) | ($ month = 10 & $ day <= 23 )){
$ StrValue = "Libra ";
} Else if ($ month = 10 & $ day> 23) | ($ month = 11 & $ day <= 22 )){
$ StrValue = "Scorpio ";
} Else if ($ month = 11 & $ day> 22) | ($ month = 12 & $ day <= 21 )){
$ StrValue = "Sagittarius ";
} Else if ($ month = 12 & $ day> 21) | ($ month = 1 & $ day <= 19 )){
$ StrValue = "Capricorn ";
    }
Return $ strValue;
     
}
Function get_shengxiao ($ cid) {// The ID card number automatically returns the corresponding Chinese Zodiac
If (! $ This-> isIdCard ($ cid) return '';
$ Start = 1901;
$ End = (int) substr ($ cid, 6, 4 );
$ X = ($ start-$ end) % 12;
$ Value = "";
If ($ x = 1 | $ x =-11) {$ value = "rat ";}
If ($ x = 0) {$ value = "";}
If ($ x = 11 | $ x =-1) {$ value = "Tiger ";}
If ($ x = 10 | $ x =-2) {$ value = "rabbit ";}
If ($ x = 9 | $ x =-3) {$ value = "Dragon ";}
If ($ x = 8 | $ x =-4) {$ value = "snake ";}
If ($ x = 7 | $ x =-5) {$ value = "horse ";}
If ($ x = 6 | $ x =-6) {$ value = "goat ";}
If ($ x = 5 | $ x =-7) {$ value = "monkey ";}
If ($ x = 4 | $ x =-8) {$ value = "chicken ";}
If ($ x = 3 | $ x =-9) {$ value = "dog ";}
If ($ x = 2 | $ x =-10) {$ value = "";}
Return $ value;
}
Function get_xingbie ($ cid) {// automatically returns gender based on ID card number
If (! $ This-> isIdCard ($ cid) return '';
$ Sexint = (int) substr ($ cid, 16, 1 );
     
Return $ sexint % 2 === 0? Female: male ';
}
 
Function checkbig18 ($ idcard) {// determines whether it is an adult
$ Year = substr ($ idcard, 6, 4 );
$ Big18 = $ year + 18;
$ Nowyear = date ("Y", time ());
If ($ big18 <= $ nowyear ){
Return true;
} Else {
Return false;
         }
    }
     
Function getVerifyBit ($ idcard_base)
    {
If (strlen ($ idcard_base )! = 17)
        {
Return false;
        }
// Weighting factor
$ Factor = array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 );
// Check code value
$ Verify_number_list = array ('1', '0', 'X', '9', '8', '7', '6', '5 ', '4', '3', '2 ');
$ Checksum = 0;
For ($ I = 0; $ I <strlen ($ idcard_base); $ I ++)
        {
$ Checksum + = substr ($ idcard_base, $ I, 1) * $ factor [$ I];
        }
$ Mod = $ checksum % 11;
$ Verify_number = $ verify_number_list [$ mod];
Return $ verify_number;
}
 
Function idcard_verify_number ($ idcard_base ){
If (strlen ($ idcard_base )! = 17) {return false ;}
// Weighting factor
$ Factor = array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 );
// Check code value
$ Verify_number_list = array ('1', '0', 'X', '9', '8', '7', '6', '5 ', '4', '3', '2 ');
$ Checksum = 0;
For ($ I = 0; $ I <strlen ($ idcard_base); $ I ++ ){
$ Checksum + = substr ($ idcard_base, $ I, 1) * $ factor [$ I];
}
 
$ Mod = $ checksum % 11;
$ Verify_number = $ verify_number_list [$ mod];
 
Return $ verify_number;
}
 
 
 
 
Function idcard_15to18 ($ idcard) {// conversion from 15 to 18
If (strlen ($ idcard )! = 15 ){
Return false;
} Else {
// If the ID card sequence code is 996 997 998 999, these are special codes for elderly people over years old
If (array_search (substr ($ idcard, 12, 3), array ('20160301', '20160301', '20160301 '))! = False ){
$ Idcard = substr ($ idcard, 0, 6). '18'. substr ($ idcard, 6, 9 );
} Else {
$ Idcard = substr ($ idcard, 0, 6). '19'. substr ($ idcard, 6, 9 );

}
 
$ Idcard = $ idcard. $ this-> idcard_verify_number ($ idcard );
Return $ idcard;
}
 
 
Function Check_Cardid ($ cardid) // determines whether the ID card size is correct.
{
$ Cardlen = strlen ($ cardid );
If ($ cardlen = 15)
 {
$ Pattern = "/^ [0-9] {15} $/"; // $ pattern = "/^ d {15} $ /";
$ Num = preg_match ($ pattern, $ cardid );
 }
Elseif ($ cardlen = 18)
 {
$ Pattern = "/^ d {17} [0-9xX] $ /";
$ Num = preg_match ($ pattern, $ cardid );
 }
Else
 {
Return false;
 }
Return $ num;
}
 
Function ChkGB2312 ($ str) {// determines whether it is Chinese.
If (preg_match ('/^ [x7f-xff] + $/', $ str )){
Return true;
} Else {
Return false;
    }
}
 
Function isIdCard ($ number) {// check whether it is an ID card number
// Convert to uppercase, if x appears
$ Number = strtoupper ($ number );
// Weighting factor
$ Wi = array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 );
// Check code string
$ Ai = array ('1', '0', 'X', '9', '8', '7', '6', '5 ', '4', '3', '2 ');
// Process the first 17 digits in a sequential loop
$ Sigma = 0;
For ($ I = 0; $ I <17; $ I ++ ){
// Extract one of the first 17 digits and convert the variable type into a real number
$ B = (int) $ number {$ I };
  
// Extract the corresponding weighting factor
$ W = $ wi [$ I];
  
// Multiply the number extracted from the ID card number and the weighting factor, and accumulate
$ Sigma + = $ B * $ w;
    }
// Calculates the serial number.
$ Snumber = $ sigma % 11;
  
// Extract the corresponding characters from the check string according to the serial number.
$ Check_number = $ ai [$ snumber];
  
If ($ number {17 }==$ check_number ){
Return true;
} Else {
Return false;
    }
}
 
 
}
?>
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.