php 验证身份证有效性,根据国家标准GB 11643-1999 15位和18位通用

来源:互联网 发布:天轩服饰淘宝商城 编辑:程序博客网 时间:2024/05/10 00:17
[php] view plain copy 在CODE上查看代码片派生到我的代码片
  1. //验证身份证是否有效  
  2. function validateIDCard($IDCard) {  
  3.     if (strlen($IDCard) == 18) {  
  4.         return check18IDCard($IDCard);  
  5.     } elseif ((strlen($IDCard) == 15)) {  
  6.         $IDCard = convertIDCard15to18($IDCard);  
  7.         return check18IDCard($IDCard);  
  8.     } else {  
  9.         return false;  
  10.     }  
  11. }  
  12.   
  13. //计算身份证的最后一位验证码,根据国家标准GB 11643-1999  
  14. function calcIDCardCode($IDCardBody) {  
  15.     if (strlen($IDCardBody) != 17) {  
  16.         return false;  
  17.     }  
  18.   
  19.     //加权因子   
  20.     $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);  
  21.     //校验码对应值   
  22.     $code = array('1''0''X''9''8''7''6''5''4''3''2');  
  23.     $checksum = 0;  
  24.   
  25.     for ($i = 0; $i < strlen($IDCardBody); $i++) {  
  26.         $checksum += substr($IDCardBody$i, 1) * $factor[$i];  
  27.     }  
  28.   
  29.     return $code[$checksum % 11];  
  30. }  
  31.   
  32. // 将15位身份证升级到18位   
  33. function convertIDCard15to18($IDCard) {  
  34.     if (strlen($IDCard) != 15) {  
  35.         return false;  
  36.     } else {  
  37.         // 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码   
  38.         if (array_search(substr($IDCard, 12, 3), array('996''997''998''999')) !== false) {  
  39.             $IDCard = substr($IDCard, 0, 6) . '18' . substr($IDCard, 6, 9);  
  40.         } else {  
  41.             $IDCard = substr($IDCard, 0, 6) . '19' . substr($IDCard, 6, 9);  
  42.         }  
  43.     }  
  44.     $IDCard = $IDCard . calcIDCardCode($IDCard);  
  45.     return $IDCard;  
  46. }  
  47.   
  48. // 18位身份证校验码有效性检查   
  49. function check18IDCard($IDCard) {  
  50.     if (strlen($IDCard) != 18) {  
  51.         return false;  
  52.     }  
  53.   
  54.     $IDCardBody = substr($IDCard, 0, 17); //身份证主体  
  55.     $IDCardCode = strtoupper(substr($IDCard, 17, 1)); //身份证最后一位的验证码  
  56.   
  57.     if (calcIDCardCode($IDCardBody) != $IDCardCode) {  
  58.         return false;  
  59.     } else {  
  60.         return true;  
  61.     }  
  62. }  
[php] view plain copy 在CODE上查看代码片派生到我的代码片
  1. 使用方法  
  2. validateIDCard('身份证');  

[php] view plain copy 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 根据身份证判断,是否满足年龄条件 
  3.  * @param type $IDCard 身份证 
  4.  * @param type $minAge 最小年龄 
  5.  */  
  6. function isMeetAgeByIDCard($IDCard$minAge) {  
  7.     $ret = validateIDCard($IDCard);  
  8.     if ($ret === FALSE) {  
  9.         return FALSE;  
  10.     }  
  11.   
  12.     if (strlen($IDCard) <= 15) {  
  13.         $IDCard = convertIDCard15to18($IDCard);  
  14.     }  
  15.   
  16.     $year = date('Y') - substr($IDCard, 6, 4);  
  17.     $monthDay = date('md') - substr($IDCard, 10, 4);  
  18.   
  19.     return ($year > $minAge || $year == $minAge && $monthDay > 0) ? TRUE : FALSE;  

0 0
原创粉丝点击