Personal ID card verification program written in PHP

Source: Internet
Author: User
Php ID card functions are useful for individuals, especially in online surveys.

Php ID card functions are useful for individuals, especially in online surveys.

 

PHP code
  1. <? Php
  2. // Calculate the ID card verification code based on the national standard GB 11643-1999
  3. Function idcard_verify_number ($ idcard_base ){
  4. If (strlen ($ idcard_base )! = 17) {return false ;}
  5. // Weighting factor
  6. $ Factor = array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 );
  7. // Check code value
  8. $ Verify_number_list = array ('1', '0', 'X', '9', '8', '7', '6', '5 ', '4', '3', '2 ');
  9. $ Checksum = 0;
  10. For ($ I = 0; $ I <strlen ($ idcard_base); $ I ++ ){
  11. $ Checksum + = substr ($ idcard_base, $ I, 1) * $ factor [$ I];
  12. }
  13. $ Mod = $ checksum % 11;
  14. $ Verify_number = $ verify_number_list [$ mod];
  15. Return $ verify_number;
  16. }
  17. // Upgrade the 15-digit ID card to 18-digit
  18. Function idcard_15to18 ($ idcard ){
  19. If (strlen ($ idcard )! = 15 ){
  20. Return false;
  21. } Else {
  22. // If the ID card sequence code is 996 997 998 999, these are special codes for elderly people over years old
  23. If (array_search (substr ($ idcard, 12, 3), array ('20160301', '20160301', '20160301 '))! = False ){
  24. $ Idcard = substr ($ idcard, 0, 6). '18'. substr ($ idcard, 6, 9 );
  25. } Else {
  26. $ Idcard = substr ($ idcard, 0, 6). '19'. substr ($ idcard, 6, 9 );
  27. }
  28. }
  29. $ Idcard = $ idcard. idcard_verify_number ($ idcard );
  30. Return $ idcard;
  31. }
  32. // Check the validity of the 18-digit ID card verification code
  33. Function idcard_checksum18 ($ idcard ){
  34. If (strlen ($ idcard )! = 18) {return false ;}
  35. $ Idcard_base = substr ($ idcard, 0, 17 );
  36. If (idcard_verify_number ($ idcard_base )! = Strtoupper (substr ($ idcard, 17, 1 ))){
  37. Return false;
  38. } Else {
  39. Return true;
  40. }
  41. }
  42. ?>


$ Idcard_base indicates the local code in the ID card. this code is only available in 18-digit ID cards, that is, the first 17 digits of the 18-digit ID card. the last digit is called the verification code.
Generally, you do not need to directly call idcard_verify_number () when using idcard_verify_number (). most common applications use the last two functions. these functions do not care about the format of the ID card string. you must perform a format check before calling the function.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.