功能算法

// 检测身份证号格式是否合法
function
CheckIsIDCard($id_card){ if(mb_strlen($id_card) != 18) return false; //校验位列表 $remainder_list = [1,0,'X',9,8,7,6,5,4,3,2]; //加权除以11的余数 $square_remainder = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]; //取得身份证号码最后一位校验位 $check_num = mb_substr($id_card,17); //身份证参与校验的前17位 $id_card = mb_substr($id_card,0,17); //参与校验的必须是数字 if(!is_numeric($id_card)) return false; $square_sum = 0;//每一位参与校验数乘以加权数的累加和 $number_sum = 0;//每一位参与校验数乘以加权除以11的余数的累加和 for ($i = 0;$i < 17;$i++){ //累加结果 $square_sum += intval($id_card[$i]) * pow(2,17-$i); //累加结果 $number_sum += intval($id_card[$i]) * $square_remainder[$i]; } //从校验位列表中获取加权乘积和得到的校验位 $check_get_square_remainder = isset($remainder_list[$square_sum%11])?$remainder_list[$square_sum%11]:-1; //从校验位列表中获取加权余数乘积和得到的校验位 $check_get_number_remainder = isset($remainder_list[$number_sum%11])?$remainder_list[$number_sum%11]:-1; if($check_get_square_remainder == $check_num && $check_get_number_remainder == $check_num) return true; return false; }

 


 /*
* 检测身份证号格式是否合法
*/
public static function validateIdCard($value)
{
$return = ['errorCode'=>0,'success'=>false,'error'=>null,'data'=>null];

if (!preg_match('/^\d{17}[0-9xX]$/', $value)) { //基本格式校验
$return['errorCode'] = ErrorCode::$dataFormatError;
$return['error'] = '身份证号格式不合法';
return $return;
}

$parsed = date_parse(substr($value, 6, 8));
if (!(isset($parsed['warning_count'])
&& $parsed['warning_count'] == 0)) { //年月日位校验
return false;
}

$base = substr($value, 0, 17);

$factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];

$tokens = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];

$checkSum = 0;
for ($i=0; $i<17; $i++) {
$checkSum += intval(substr($base, $i, 1)) * $factor[$i];
}

$mod = $checkSum % 11;
$token = $tokens[$mod];

$lastChar = strtoupper(substr($value, 17, 1));

if($lastChar !== $token){
$return['errorCode'] = ErrorCode::$dataFormatError;
$return['error'] = '身份证号格式不合法';
return $return;
}
}
 
    /*
     * 获取用户真实ip
     */
    public static function get_real_ip(){
        $ip=false;
        if(!empty($_SERVER['HTTP_CLIENT_IP'])){
            $ip=$_SERVER['HTTP_CLIENT_IP'];
        }
        if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
            $ips=explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
            if($ip){ array_unshift($ips, $ip); $ip=FALSE; }
            for ($i=0; $i < count($ips); $i++){
                if(!preg_match ('/^(10│172.16│192.168)./', $ips[$i])){
                    $ip=$ips[$i];
                    break;
                }
            }
        }
        return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
    }

 

  /**
     * 验证ip地址
     * @param        string $ip , ip地址
     * @return        bool    正确返回true, 否则返回false
     */
    public static function checkIP($ip)
    {
        $ip = trim($ip);
        $pt = '/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/';
        if (preg_match($pt, $ip) === 1) {
            return true;
        }
        return false;
    }

 

posted @ 2019-05-11 08:55  牛奔  阅读(354)  评论(0编辑  收藏  举报