Home Backend Development PHP Tutorial 身份证号验证(兼容15,18位)_PHP教程

身份证号验证(兼容15,18位)_PHP教程

Jul 13, 2016 am 10:37 AM
php code compatible Can register fragment of identity verify

份证号验证(兼容15,18位) ,注册验证的时候可以用上。    

代码片段(1)

[代码] [PHP]代码

view source print? 01 //比较菜鸟 第一次分享代码   02 //身份证号验证 03     protected function checkIdCard(){ 04         if(empty($_POST['idcard'])){ 05             return false; 06         } 07         $idcard $_POST['idcard']; 08         $City array(11=>"北京",12=>"天津",13=>"河北",14=>"山西",15=>"内蒙古",21=>"辽宁",22=>"吉林",23=>"黑龙江",31=>"上海",32=>"江苏",33=>"浙江",34=>"安徽",35=>"福建",36=>"江西",37=>"山东",41=>"河南",42=>"湖北",43=>"湖南",44=>"广东",45=>"广西",46=>"海南",50=>"重庆",51=>"四川",52=>"贵州",53=>"云南",54=>"西藏",61=>"陕西",62=>"甘肃",63=>"青海",64=>"宁夏",65=>"新疆",71=>"台湾",81=>"香港",82=>"澳门",91=>"国外"); 09         $iSum = 0; 10         $idCardLength <code class="functions">strlen($idcard); 11         //长度验证 12         if(!preg_match('/^\d{17}(\d|x)$/i',$idcardand!preg_match('/^\d{15}$/i',$idcard)) 13         { 14             return false; 15         } 16         //地区验证 17         if(!array_key_exists(intval(substr($idcard,0,2)),$City)) 18         { 19            return false; 20         } 21         // 15位身份证验证生日,转换为18位 22         if ($idCardLength == 15) 23         { 24             $sBirthday '19'.substr($idcard,6,2).'-'.substr($idcard,8,2).'-'.substr($idcard,10,2); 25             $d new DateTime($sBirthday); 26             $dd $d->format('Y-m-d'); 27             if($sBirthday != $dd) 28             { 29                 return false; 30             } 31             $idcard substr($idcard,0,6)."19".substr($idcard,6,9);//15to18 32             $Bit18 = getVerifyBit($idcard);//算出第18位校验码 33             $idcard $idcard.$Bit18; 34         } 35         // 判断是否大于2078年,小于1900年 36         <code class="variable">$year substr($idcard,6,4); 37         if (<code class="variable">$year<code class="variable">$year>2078 ) 38         { 39             return false; 40         } 41   42         //18位身份证处理 43         $sBirthday substr($idcard,6,4).'-'.substr($idcard,10,2).'-'.substr($idcard,12,2); 44         $d new DateTime($sBirthday); 45         $dd $d->format('Y-m-d'); 46         if($sBirthday != $dd) 47          { 48             return false; 49          } 50         //身份证编码规范验证 51         $idcard_base substr($idcard,0,17); 52         if(strtoupper(substr($idcard,17,1)) != getVerifyBit($idcard_base)) 53         { 54            return false; 55         } 56         return $_POST['idcard']; 57     } 58   59     // 计算身份证校验码,根据国家标准GB 11643-1999 60     function getVerifyBit($idcard_base) 61     { 62         if(<code class="functions">strlen($idcard_base) != 17) 63         { 64             return false; 65         } 66         //加权因子 67         $factor array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 68         //校验码对应值 69         $verify_number_list array('1''0''X''9''8''7''6''5''4','3''2'); 70         $checksum = 0; 71         for ($i = 0; $i <code class="functions">strlen($idcard_base); $i++) 72         { 73             $checksum += substr($idcard_base$i, 1) * $factor[$i]; 74         } 75         $mod $checksum % 11; 76         $verify_number $verify_number_list[$mod]; 77         return $verify_number; 78     }

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/735174.htmlTechArticle份证号验证(兼容15,18位) ,注册验证的时候可以用上。 代码片段 (1) [代码] [PHP]代码 view source print? 01 //比较菜鸟 第一次分享代码 02 //身份证...
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1263
29
C# Tutorial
1237
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles