Home Backend Development PHP Tutorial Implementation of ID card verification code calculation method based on PHP

Implementation of ID card verification code calculation method based on PHP

Jun 01, 2018 pm 03:45 PM
php accomplish ID card

This article will share with you an attempt to use the PHP language to implement the ID card verification code to determine whether the user's ID number is correct.

The meaning of each digit of the Chinese (Mainland) citizen ID number. Many articles on the Internet have introduced it, so I won’t go into details here. The last digit of the ID number is the check code, which is calculated based on the first 17 digits. The algorithm is roughly like this: multiply each number in the first 17 digits by a series of weighting factors, and then calculate the sum of these products; use the sum of these products modulo 11 as the serial number, and finally extract it from a check code string Output the characters corresponding to the serial number. Of course, there are many articles on the Internet teaching you to calculate this check code. Below we will try to use PHP language to complete this work. It may be used in PHP development, such as verifying whether the user's ID number is correct.

Assume that the first 17 digits of a Chinese (Mainland) citizen’s ID card number are this: 44010221990101001 (Note: This person was born in 2199), then we try to write a few lines of PHP code according to the above algorithm to complete Calculation of checksum. In order to make it easier for everyone to understand, I used simpler statements, please look at the code:

<?php
//身份证号码前17位,可以从各种数据源中获得(如数据库、用户提交的表单等)
$body = '44010221990101001';
//加权因子
$wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
//校验码串
$ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
//按顺序循环处理前17位
for ($i = 0;$i < 17;$i++) {
//提取前17位的其中一位,并将变量类型转为实数
$b = (int) $body{$i};
//提取相应的加权因子
$w = $wi[$i];
//把从身份证号码中提取的一位数字和加权因子相乘,并累加
$sigma += $b * $w;
}
//计算序号
$number = $sigma % 11;
//按照序号从校验码串中提取相应的字符。
$check_number = $ai[$number];
//输出
echo $body.$check_number;
?>
Copy after login

After running the above code, you can calculate the number of the ID card The check code is 9. You can try using the first 17 digits of your ID card.

If you understand the above example, you can merge some statements of this code, remove unnecessary variables, and optimize to get the following code:

<?php
$body = '44010221990101001';
$wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
$ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
for ($i = 0;$i < 17;$i++) {
$sigma += ((int) $body{$i}) * $wi[$i];
}
echo $body.$ai[($sigma % 11)];
?>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Detailed examples of how to use static variables in PHP

thinkPHP’s method of implementing the order digital reminder function

Notes on accessing PHP Sesame Credit

##

The above is the detailed content of Implementation of ID card verification code calculation method based on PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles