欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

常用的检查函数

程序员文章站 2022-03-18 08:26:11
...

1. [文件] fun.php

<?php
//安全协议判断
function ssl()
{
    if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
        return true;
    } elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) {
        return true;
    }
    return false;
}
// 代理访问检测
function proxy()
{
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) || !empty($_SERVER['HTTP_VIA']) || !empty($_SERVER['HTTP_PROXY_CONNECTION']) || !empty($_SERVER['HTTP_USER_AGENT_VIA'])) {
        return true;
    }
    return false;
}
//机器人检测
function robot()
{
    static $_robot = null;
    if (is_null($_robot)) {
        $spiders  = 'Bot|Crawl|Spider|slurp|sohu-search|lycos|robozilla';
        $browsers = 'MSIE|Netscape|Opera|Konqueror|Mozilla';
        if (preg_match("/($browsers)/", $_SERVER['HTTP_USER_AGENT'])) {
            $_robot = false;
        } elseif (preg_match("/($spiders)/", $_SERVER['HTTP_USER_AGENT'])) {
            $_robot = true;
        } else {
            $_robot = false;
        }
    }
    return $_robot;
}

//浏览器检查
function browser($browser = '')
{
    $browsers = array(
        ".*opera[ /]([0-9.]{1,10})" => "opera",
        ".compatible; MSIE[ /]([0-9.]{1,10}).*" => "ie",
        ".*Firefox/([0-9.+]{1,10})" => "firefox",
        ".Version/([0-9.+]{1,10})" => "safari",
        ".Chrome/([0-9.+]{1,10})" => "chrome"
    );

    $browser_info = array();
    foreach ($browsers as $match => $browser_name) {
        if (preg_match('#' . $match . '#i', $_SERVER["HTTP_USER_AGENT"], $matches)) {
            $browser_info[] = $browser_name;
            $browser_info[] = $matches[1];
            $browser_info[] = $browser_name . ' ' . $matches[1];
        }
    }
	return $browser_info;
    
}

// 判断Ajax方式提交    
function ajax()
{
    if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
        if ('xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']))
            return true;
    }
    return false;
}
function url($v,$filter=false)
{
    if($filter) $v = filter_var($v, FILTER_SANITIZE_URL);
    if (filter_var($v, FILTER_VALIDATE_URL)) {
        return $v;
    } else {
        return false;
    }
}
function ip($v)
{
    if (filter_var($v, FILTER_VALIDATE_IP)) {
        return $v;
    } else {
        return false;
    }
}
function email($v,$filter=false)
{
    if($filter) $v = filter_var($v, FILTER_SANITIZE_EMAIL);
    if (filter_var($v, FILTER_VALIDATE_EMAIL)) {
        return $v;
    } else {
        return false;
    }
}




//身份证检查
function card($id_card)
{
    include_once('card.php');
    if (strlen($id_card) == 18 && idcard_checksum18($id_card)) {
        return $id_card;
    } elseif (strlen($id_card) == 15) {
        $id_card2 = idcard_15to18($id_card);
        if (idcard_checksum18($id_card2)) {
            return $id_card;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

//银行卡号检查
//位数16-20
function bank_card($card)
{
   if (!preg_match('#^\d{16,20}$#', $card)) {
        return false;
    } 
	$return='';
    $arr = include_once('bank_card.php');
    foreach ($arr as $k => $v) {
        if (preg_match('#'.$k.'#U',$card)){
			$v=explode('-',$v,3);
			$return['card']=$card;
			$return['name']=$v[0];
			$return['type']=$v[2];
			return $return;
		}
            
    }
    return false;
}

//手机号码检查
function shouji($phone)
{
    if (preg_match('#^1[3458]{1}\d{9}$#', $phone)) {
        return $phone;
    } else {
        return false;
    }
}



//检查字符
function char($char,$length='1,6',$charlist='')
{
    if (empty($charlist)) {
        $charlist='A-Za-z0-9_';
    }
	if(preg_match('#^['.$charlist.']{'.$length.'}$#',$char)){
		return $char;
	}else{
		return false;
	}
}


//检查验证码
function yzm($yzm)
{
    if (empty($_SESSION['code']) || empty($yzm)) {
        return false;
    }
    if ($_SESSION['code'] == substr(md5(strtolower($yzm)), 8, 28)) {
        return true;
    } else {
        return false;
    }
}

2. [文件] card.php

<?php
// 计算身份证校验码,根据国家标准GB 11643-1999 
function idcard_verify_number($idcard_base)
{
    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;
}
// 将15位身份证升级到18位 
function idcard_15to18($idcard)
{
    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 . idcard_verify_number($idcard);
    return $idcard;
}
// 18位身份证校验码有效性检查 
function idcard_checksum18($idcard)
{
    if (strlen($idcard) != 18) {
        return false;
    }
    $idcard_base = substr($idcard, 0, 17);
    if (idcard_verify_number($idcard_base) != strtoupper(substr($idcard, 17, 1))) {
        return false;
    } else {
        return true;
    }
}
?>


3. [文件] bank_card.rar

常用的检查函数bank_card.rar

4. [文件] check.php

<?php
require_once('../lib/check/fun.php');
//安全协议判断
var_dump(ssl());
// 代理访问检测
var_dump(proxy());
//机器人检测
var_dump(robot());
//浏览器检查
var_dump(browser('chrome'));
// 判断Ajax方式提交    
var_dump(ajax());
//url 
//参数url,是否预先过滤url非法字符
var_dump(url('http://127.0.0.1/lizhi/check.php',true));
//ip 
//参数 待检ip
var_dump(ip('11.444.55.66'));
//email
//参数email,是否预先过滤email非法字符
var_dump(email('24556@qq.com',true));
//身份证检查
var_dump(card('350128198506275555'));
//银行卡号检查
//位数16-20
var_dump(bank_card('6258081480887777'));
//手机号码检查
var_dump(shouji('13052561263'));

//检查字符串
//参数 待检字符串 匹配长度 匹配字符集
var_dump(char('ee1301263','1,20'));
//检查验证码
yzm('sss');