*/**
* 验证身份证是否有效
* @param type $sid
*/
private function check_IDCard($sid) {
if (strlen($sid) != 15 && strlen($sid) != 18) {
//如果位数不符合15 或者18
return FALSE;
}
if (strlen($sid) == 15) {
$wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
$sid = substr_replace($sid, 19, 6, 0);
$count = array();
$sid[17] = 0; //定义第18位
for ($i = 0; $i < 17; ++$i) {
$count[] = $sid[$i] * $wi[$i];
}
$sum = array_sum($count) % 11;
$zuihou = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
$sid = substr_replace($sid, $zuihou[$sum], 17, 1);
}
$wi18 = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
$sum2 = array();
for ($j = 0; $j < 17; $j++) {
$sum2[] = $sid[$j] * $wi18[$j];
}
$sum2 = array_sum($sum2) % 11;
$yz = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
if ($yz[$sum2] == $sid[17]) {
//如果最后一位和验证号一样,则返回真
return TRUE;
} else {
return false;
}
}
PHP 身份证验证
最新推荐文章于 2020-11-14 15:23:35 发布