/**
* Check per pin
*
* @param string $pin
* @return array
*/
public function checkPerPin($pin)
{
$length = strlen($pin);
if ($length == 15) {
$var ['pin'] = $this->IdCard15To18($pin);
}
if ($length == 18) {
if (! $this->IdcardCheckSum18($pin)) {
return false;
}
}
if ($length != 18 && $length != 15) {
return false;
}
return true;
}
/**
* 将15位号码升至18位
*
* @param String $idcard
* @return String
*/
function IdCard15To18($idcard) // 功能:将15位身份证升级到18位
{
if (strlen($idcard ) != 15){
return false;
} else {
// 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码
if (array_search ( substr ( $idcard, 12, 3 ), array ('996', '997', '998', '999' ) ) !== false) {
$idcard = substr ( $idcard, 0, 6 ) . '18' . substr ( $idcard, 6, 9 );
} else {
$idcard = substr ( $idcard, 0, 6 ) . '19' . substr ( $idcard, 6, 9 );
}
}
$idcard = $idcard . ($this->IdcardVerifyNumber ( $idcard ));
return $idcard;
}
/**
* 18位身份证号码的有效性检查
*
* @param String $idcard
* @return Boolean
*/
function IdcardCheckSum18($idcard)
{
if (strlen ( $idcard ) != 18) {
return false;
}
$idcard_base = substr ( $idcard, 0, 17 );
if (($this->IdcardVerifyNumber ( $idcard_base )) != strtoupper ( substr ( $idcard, 17, 1 ) )) {
return false;
} else {
return true;
}
}
/**
* 计算身份证校验码
*
* @param String $idcard_base
* @return String
*/
function IdcardVerifyNumber($idcard_base) // 功能:计算身份证校验码,根据国家标准GB 11643-1999
{
if (strlen ( $idcard_base ) != 17)
{
return false;
}
$factor = array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 );
$verify_number_list = array ('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' );
$checksum = 0;
for($i = 0; $i < strlen ( $idcard_base ); $i ++)
{
$checksum += substr ( $idcard_base, $i, 1 ) * $factor [$i];
}
$mod = $checksum % 11;
$verify_number = $verify_number_list [$mod];
return $verify_number;
}