Home > php教程 > PHP源码 > body text

常用的检查函数

PHP中文网
Release: 2016-05-23 08:39:39
Original
1204 people have browsed it

1. [文件]     fun.php 

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

//浏览器检查
function browser($browser = &#39;&#39;)
{
    $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(&#39;#&#39; . $match . &#39;#i&#39;, $_SERVER["HTTP_USER_AGENT"], $matches)) {
            $browser_info[] = $browser_name;
            $browser_info[] = $matches[1];
            $browser_info[] = $browser_name . &#39; &#39; . $matches[1];
        }
    }
	return $browser_info;
    
}

// 判断Ajax方式提交    
function ajax()
{
    if (isset($_SERVER[&#39;HTTP_X_REQUESTED_WITH&#39;])) {
        if (&#39;xmlhttprequest&#39; == strtolower($_SERVER[&#39;HTTP_X_REQUESTED_WITH&#39;]))
            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(&#39;card.php&#39;);
    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(&#39;#^\d{16,20}$#&#39;, $card)) {
        return false;
    } 
	$return=&#39;&#39;;
    $arr = include_once(&#39;bank_card.php&#39;);
    foreach ($arr as $k => $v) {
        if (preg_match(&#39;#&#39;.$k.&#39;#U&#39;,$card)){
			$v=explode(&#39;-&#39;,$v,3);
			$return[&#39;card&#39;]=$card;
			$return[&#39;name&#39;]=$v[0];
			$return[&#39;type&#39;]=$v[2];
			return $return;
		}
            
    }
    return false;
}

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



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


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

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(
        &#39;1&#39;,
        &#39;0&#39;,
        &#39;X&#39;,
        &#39;9&#39;,
        &#39;8&#39;,
        &#39;7&#39;,
        &#39;6&#39;,
        &#39;5&#39;,
        &#39;4&#39;,
        &#39;3&#39;,
        &#39;2&#39;
    );
    $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(
            &#39;996&#39;,
            &#39;997&#39;,
            &#39;998&#39;,
            &#39;999&#39;
        )) !== false) {
            $idcard = substr($idcard, 0, 6) . &#39;18&#39; . substr($idcard, 6, 9);
        } else {
            $idcard = substr($idcard, 0, 6) . &#39;19&#39; . 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;
    }
}
?>
Copy after login


3. [文件] bank_card.rar

常用的检查函数bank_card.rar

4. [文件] check.php

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

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

                               

                   

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!