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


Posted in PHP onMay 03, 2017

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

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

身份证号码验证

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 com\jdk5\blog\IDValidator;
 
class IDValidator {
 private static $GB2260;
 private static $instance;
 private static $cache = array();
 private static $util;
 function __construct() {
 if (!class_exists("com\jdk5\blog\IDValidator\GB2260")){
 include 'GB2260.php';
 }
 if (!class_exists("com\jdk5\blog\IDValidator\util")){
 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 = com\jdk5\blog\IDValidator\IDValidator::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"));

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
PHP中的日期处理方法集锦
Jan 02 PHP
如何在symfony中导出为CSV文件中的数据
Oct 06 PHP
php如何调用webservice应用介绍
Nov 24 PHP
PHP下打开phpMyAdmin出现403错误的问题解决方法
May 23 PHP
深入PHP autoload机制的详解
Jun 09 PHP
php统计时间和内存使用情况示例分享
Mar 13 PHP
php中print(),print_r(),echo()的区别详解
Dec 01 PHP
php中异常处理方法小结
Jan 09 PHP
php+mysqli批量查询多张表数据的方法
Jan 29 PHP
PHP基于swoole多进程操作示例
Aug 12 PHP
Yii框架应用组件用法实例分析
May 15 PHP
open_basedir restriction in effect. 原因与解决方法
Mar 14 PHP
PHP基于Redis消息队列实现发布微博的方法
May 03 #PHP
Laravel5中Cookie的使用详解
May 03 #PHP
[原创]php实现数组按拼音顺序排序的方法
May 03 #PHP
PHP基于ICU扩展intl快速实现汉字转拼音及按拼音首字母分组排序的方法
May 03 #PHP
php中的异常和错误浅析
May 03 #PHP
利用php-cli和任务计划实现刷新token功能的方法
May 03 #PHP
利用php-cli和任务计划实现订单同步功能的方法
May 03 #PHP
You might like
php 来访国内外IP判断代码并实现页面跳转
2009/12/18 PHP
ThinkPHP实现一键清除缓存方法
2014/06/26 PHP
微信公众平台天气预报功能开发
2014/07/06 PHP
PHP面试题之文件目录操作
2015/10/15 PHP
Laravel路由设定和子路由设定实例分析
2016/03/30 PHP
PHP云打印类完整示例
2016/10/15 PHP
php微信开发之音乐回复功能
2018/06/14 PHP
Laravel关联模型中过滤结果为空的结果集(has和with区别)
2018/10/18 PHP
thinkPHP5框架路由常用知识点汇总
2019/09/15 PHP
javascript 放大镜 v1.0 基于Yui2 实现的放大镜效果
2010/03/08 Javascript
DIV始终居中的js代码
2014/02/17 Javascript
Jquery插件实现点击获取验证码后60秒内禁止重新获取
2015/03/13 Javascript
JS实现多物体运动的方法详解
2018/01/23 Javascript
JavaScript实现微信红包算法及问题解决方法
2018/04/26 Javascript
Vue 框架之键盘事件、健值修饰符、双向数据绑定
2018/11/14 Javascript
Vue组件间通信方法总结(父子组件、兄弟组件及祖先后代组件间)
2019/04/17 Javascript
基于nodejs的微信JS-SDK简单应用实现
2019/05/21 NodeJs
JS扁平化输出数组的2种方法解析
2019/09/17 Javascript
react quill中图片上传由默认转成base64改成上传到服务器的方法
2019/10/30 Javascript
js表达式与运算符简单操作示例
2020/02/15 Javascript
[02:36]DOTA2亚洲邀请赛小组赛精彩集锦:奇迹哥卡尔秀翻全场
2017/03/28 DOTA
Python获取网页上图片下载地址的方法
2015/03/11 Python
Python中获取对象信息的方法
2015/04/27 Python
基于python socketserver框架全面解析
2017/09/21 Python
python基础学习之如何对元组各个元素进行命名详解
2018/07/12 Python
浅谈Pycharm调用同级目录下的py脚本bug
2018/12/03 Python
自定义实现 PyQt5 下拉复选框 ComboCheckBox的完整代码
2020/03/30 Python
Python GUI编程学习笔记之tkinter界面布局显示详解
2020/03/30 Python
Python实现从N个数中找到最大的K个数
2020/04/02 Python
请写出一段Python代码实现删除一个list里面的重复元素
2015/12/29 面试题
优秀共产党员先进事迹
2014/01/27 职场文书
终止或解除劳动合同及劳动关系的证明书
2014/10/06 职场文书
2016年中秋祝酒词
2015/11/26 职场文书
为什么 Nginx 比 Apache 更牛逼
2021/03/31 Servers
django注册用邮箱发送验证码的实现
2021/04/18 Python
NGINX 权限控制文件预览和下载的实现原理
2022/01/18 Servers