当前位置:首页 > PHP教程 > PHP常见问题

PHP实现中国公民身份证号码有效性验证示例代码

本文将使用java实现中国公民(15位或者18位)身份证号码的相关验证,功能如下:

  • 身份证号有效性验证
  • 分析详细身份证信息
  • 生成一个虚拟的省份证号码。
  • 身份证号码验证

    1、号码的结构 公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。

    2、地址码(前六位数)

    表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按gb/t2260的规定执行。

    3、出生日期码(第七位至十四位)

    表示编码对象出生的年、月、日,按gb/t7408的规定执行,年、月、日代码之间不用分隔符。

    4、顺序码(第十五位至十七位)

    表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号, 顺序码的奇数分配给男性,偶数分配给女性。

    5、校验码(第十八位数)

    (1)十七位数字本体码加权求和公式 s = sum(ai * wi), i = 0, … , 16 ,先对前17位数字的权求和

    ai:表示第i位置上的身份证号码数字值

    wi:表示第i位置上的加权因子 wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2

    (2)计算模 y = mod(s, 11)

    (3)通过模得到对应的校验码 y: 0 1 2 3 4 5 6 7 8 9 10 校验码: 1 0 x 9 8 7 6 5 4 3 2

    idvalidator.php

    <?php
    namespace comjdk5blogidvalidator;
     
    class idvalidator {
     private static $gb2260;
     private static $instance;
     private static $cache = array();
     private static $util;
     function __construct() {
     if (!class_exists("comjdk5blogidvalidatorgb2260")){
     include 'gb2260.php';
     }
     if (!class_exists("comjdk5blogidvalidatorutil")){
     include 'util.php';
     }
     self::$gb2260 = gb2260::getgb2260 ();
     self::$util = util::getinstance();
     }
     public static function getinstance() {
     if (is_null ( self::$instance )) {
     self::$instance = new idvalidator ();
     }
     return self::$instance;
     }
     function isvalid($id) {
     $code = self::$util->checkarg ( $id );
     if ($code === false) {
     return false;
     }
     // 查询cache
     if (isset ( self::$cache [ $id ] ) && self::$cache [$id] ['valid'] !== false) {
     return self::$cache [$id] ['valid'];
     } else {
     if (! isset ( self::$cache [ $id ] )) {
     self::$cache [$id] = array ();
     }
     }
     
     $addr = substr ( $code ['body'], 0, 6 );
     $birth = $code ['type'] === 18 ? substr ( $code ['body'], 6, 8 ) :
     substr ( $code ['body'], 6, 6 );
     $order = substr ( $code ['body'], - 3 );
     
     if (! (self::$util->checkaddr ( $addr ) && self::$util->checkbirth ( $birth ) &&
     self::$util->checkorder ( $order ))) {
     self::$cache [$id] ['valid'] = false;
     return false;
     }
     
     // 15位不含校验码,到此已结束
     if ($code ['type'] === 15) {
     self::$cache [$id] ['valid'] = true;
     return true;
     }
     
     /* 校验位部分 */
     
     // 位置加权
     $posweight = array ();
     for($i = 18; $i > 1; $i --) {
     $wei = self::$util->weight ( $i );
     $posweight [$i] = $wei;
     }
     
     // 累加body部分与位置加权的积
     $bodysum = 0;
     $bodyarr = str_split( $code ['body'] );
     for($j = 0; $j < count ( $bodyarr ); $j ++) {
     $bodysum += (intval ( $bodyarr [$j], 10 ) * $posweight [18 - $j]);
     }
     
     // 得出校验码
     $checkbit = 12 - ($bodysum % 11);
     if ($checkbit == 10) {
     $checkbit = 'x';
     } else if ($checkbit > 10) {
     $checkbit = $checkbit % 11;
     }
     // 检查校验码
     if ($checkbit != $code ['checkbit']) {
     self::$cache [$id] ['valid'] = false;
     return false;
     } else {
     self::$cache [$id] ['valid'] = true;
     return true;
     }
     }
     // 分析详细信息
     function getinfo ($id) {
     // 号码必须有效
     if ($this->isvalid($id) === false) {
     return false;
     }
     // todo 复用此部分
     $code = self::$util->checkarg($id);
     
     // 查询cache
     // 到此时通过isvalid已经有了cache记录
     if (isset(self::$cache[$id]) && isset(self::$cache[$id]['info'])) {
     return self::$cache[$id]['info'];
     }
     
     $addr = substr($code['body'], 0, 6);
     $birth = ($code['type'] === 18 ? substr($code['body'], 6, 8) :
     substr($code['body'], 6, 6));
     $order = substr($code['body'], -3);
     
     $info = array();
     $info['addrcode'] = $addr;
     if (self::$gb2260 !== null) {
     $info['addr'] = self::$util->getaddrinfo($addr);
     }
     $info ['birth'] = ($code ['type'] === 18 ? (substr ( $birth, 0, 4 ) . '-' . substr ( $birth, 4, 2 ) . '-' . substr ( $birth, - 2 )) : ('19' . substr ( $birth, 0, 2 ) . '-' . substr ( $birth, 2, 2 ) . '-' . substr ( $birth, - 2 )));
     $info['sex'] = ($order % 2 === 0 ? 0 : 1);
     $info['length'] = $code['type'];
     if ($code['type'] === 18) {
     $info['checkbit'] = $code['checkbit'];
     }
     
     // 记录cache
     self::$cache[$id]['info'] = $info;
     
     return $info;
     }
     
     // 仿造一个号
     function makeid ($isfifteen=false) {
     // 地址码
     $addr = null;
     if (self::$gb2260 !== null) {
     $loopcnt = 0;
     while ($addr === null) {
     // 防止死循环
     if ($loopcnt > 50) {
      $addr = 110101;
      break;
     }
     $prov = self::$util->str_pad(self::$util->rand(66), 2, '0');
     $city = self::$util->str_pad(self::$util->rand(20), 2, '0');
     $area = self::$util->str_pad(self::$util->rand(20), 2, '0');
     $addrtest = $prov . $city . $area;
     if (isset(self::$gb2260[$addrtest])) {
      $addr = $addrtest;
      break;
     }
     $loopcnt ++;
     }
     } else {
     $addr = 110101;
     }
     
     // 出生年
     $yr = self::$util->str_pad(self::$util->rand(99, 50), 2, '0');
     $mo = self::$util->str_pad(self::$util->rand(12, 1), 2, '0');
     $da = self::$util->str_pad(self::$util->rand(28, 1), 2, '0');
     if ($isfifteen) {
     return $addr . $yr . $mo . $da
     . self::$util->str_pad(self::$util->rand(999, 1), 3, '1');
     }
     
     $yr = '19' . $yr;
     $body = $addr . $yr . $mo . $da . self::$util->str_pad(self::$util->rand(999, 1), 3, '1');
     
     // 位置加权
     $posweight = array();
     for ($i = 18; $i > 1; $i--) {
     $wei = self::$util->weight($i);
     $posweight[$i] = $wei;
     }
     
     // 累加body部分与位置加权的积
     $bodysum = 0;
     $bodyarr = str_split($body);
     for ($j = 0; $j < count($bodyarr); $j++) {
     $bodysum += (intval($bodyarr[$j], 10) * $posweight[18 - $j]);
     }
     
     // 得出校验码
     $checkbit = 12 - ($bodysum % 11);
     if ($checkbit == 10) {
     $checkbit = 'x';
     } else if ($checkbit > 10) {
     $checkbit = $checkbit % 11;
     }
     return ($body . $checkbit);
     }
    }

    调用

    <?php
    header("content-type: text/html; charset=utf-8");
    
    include 'idvalidator.php';
    $v = comjdk5blogidvalidatoridvalidator::getinstance();
    
    //生成一个18位身份证号
    $id = $v->makeid();
    //获取身份证信息
    $info = $v->getinfo($id);
    var_dump($info);
    //生成一个15位身份证号
    $id = $v->makeid(true);
    $info = $v->getinfo($id);
    var_dump($info);
    
    //验证身份证号是否正确
    var_dump($v->isvalid("123456789012345678"));
    

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。


    【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!