常用Validate验证类

<?php

    class Validate {

        public static $MD5KEY='Fw8LK@A3aFDS#OM1qQC*GbY';
        const TOKEN_EXPIRE=604800;//TOKEN过期时间(7天)

        /**
         * 生成邀请码
         * @param int $uid
         * @return string
         */
        public static function inviteCode($uid){
            $code=base_convert($uid, 10, 16);
            $code=base_convert($code, 20, 36);
            $code=strtoupper(substr(md5($uid.self::$MD5KEY), 0,3).$code);
            return $code;
        }

        /**
         * 验证邀请码
         * @param string $code
         * @return string|boolean
         */
        public static function inviteUid($code){
            $code=strtolower($code);
            $hash = substr($code, 0,3);
            $code = substr($code, 3);
            $uid=base_convert($code, 36, 20);//实际得到是UID的16进制
            $uid=base_convert($uid, 16, 10);
            if (substr(md5($uid.self::$MD5KEY), 0,3)==$hash){
                return $uid;
            }
            return 0;
        }

        public static function password($pwd){
            $p = md5($pwd);
            return md5(substr($p, 16, 8).substr($p, 0, 8).substr($p, 24, 8).substr($p, 8, 8));
        }

        /**
         * 检查用户token
         * @param int $uid
         * @param string $token
         * @return boolean
         */
        public static function checkToken_1($uid,$token){
            $res=false;
            for ($i=0;$i<7;$i++){//7天过期
                $daytime = strtotime("-{$i} day");
                //dump(date('Ymd',$daytime));

                $t=self::token($uid,date('Ymd',$daytime));

                if ($token===$t){
                    $res=true;
                    break;
                }
            }

            return $res;
        }

        /**
         * 生成用户token
         * @param int $uid
         * @param string $day
         */
        public static function token_1($uid,$day=''){
            //$week=date('W');//7天过期

            if (empty($day)){
                $day = date('Ymd');
            }

            $str=md5(self::$MD5KEY.$uid.$day);
            $str2=md5($str);

            $str3=array();
            for ($i=0;$i<strlen($str);$i++){
                $str3[]=$str[$i];
                if ($i>0&&$uid%$i==0){
                    $str3[]=$str2[$i];
                }
            }

            return implode('', $str3);
        }

        /**
         * 检查用户token
         * @param int $uid
         * @param string $token
         * @return boolean
         */
        public static function checkToken($uid,$token){
            if ($uid<1){
                return false;
            }
            $dec_strarr = str_split($token,4);
            $parsenum=count($dec_strarr);
            if ($parsenum<10){
                return false;
            }
            //dump($dec_strarr);

            $dec_timestr='';
            $dec_sign='';
            //==========解析时间==========
            for ($j=0;$j<$parsenum;$j++){
                if ($j<10){
                    $pos=$uid%($j+1);
                    if ($pos==0){
                        $dec_timestr.=substr($dec_strarr[$j], -1);
                        $dec_sign.=substr($dec_strarr[$j], 0,3);
                    }else{
                        $dec_timestr.=substr($dec_strarr[$j], 0,1);
                        $dec_sign.=substr($dec_strarr[$j], -3);
                    }
                }else{
                    $dec_sign.=$dec_strarr[$j];
                }
            }
            //dump($dec_timestr);
            //dump($dec_sign);
            if (!is_numeric($dec_timestr)){
                return false;
            }

            if (time()-$dec_timestr>self::TOKEN_EXPIRE){//七天有效期
                return false;
            }

            $str=md5(self::$MD5KEY.$uid.$dec_timestr);
            $str2=md5($str);

            $strarr=array();
            for ($i=0;$i<strlen($str);$i++){
                $strarr[]=$str[$i];
                if ($i>0&&$uid%$i==0){
                    $strarr[].=$str2[$i];
                }
            }
            $sign=implode('', $strarr);

            return $dec_sign===$sign;
        }

        /**
         * 生成用户token
         * @param int $uid
         * @param string $day
         */
        public static function token($uid){
            $time=time();

            $str=md5(self::$MD5KEY.$uid.$time);
            $str2=md5($str);

            $strarr=array();
            for ($i=0;$i<strlen($str);$i++){
                $strarr[]=$str[$i];
                if ($i>0&&$uid%$i==0){
                    $strarr[].=$str2[$i];
                }
            }
            $sign=implode('', $strarr);

            //==========插入时间==========
            $t1=str_split($sign,3);
            $timestr=strval($time);

            $uposarr=array();
            for ($j=0;$j<10;$j++){
                $pos=$uid%($j+1);
                if ($pos==0){
                    $t1[$j]=$t1[$j].$timestr[$j];
                }else{
                    $t1[$j]=$timestr[$j].$t1[$j];
                }
            }

            $token=implode('', $t1);

            return $token;
        }

        public static function checkUserName($userName = ''){
            $res = array('code'=>0,'msg'=>'');
            if (empty($userName)) {
                $res['msg']='请输入用户名';
                return $res;
            }

            if (preg_match ( '/^[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']$/', $userName )) {
                $res['msg']='';
                return $res;
                return ResponseUtil::format(-3,'','用户名不能包含特殊字符');
            }

            if (preg_match ( '/^[0-9]{1,}$/', $userName )) {
                $res['msg']='用户名不能全是数字!';
                return $res;
            }

            if (preg_match ( '/^[0-9].*$/', $userName )) {
                $res['msg']='用户名第一个字符不能为数字!';
                return $res;
            }
            if (preg_match ( '/^(open).*$/', $userName )) {
                $res['msg']='用户名不能使用open关键词!';
                return $res;
            }

            /* if (preg_match ( "/[^a-zA-Z0-9\x{4e00}-\x{9fa5}]+/u", $userName )) {
                $res['msg']='请确保用户名由数字、字母组成,且第一位不能为数字!';
                return $res;
            } */

            if (preg_match ( "/[^a-zA-Z0-9_]+/u", $userName )) {
                $res['msg']='请确保用户名由数字、字母组成,且第一位不能为数字!';
                return $res;
            }

            if (mb_strlen($userName,'utf-8') < 4 || mb_strlen($userName,'utf-8') > 20) {
                $res['msg']='用户名长度4-20个字符!';
                return $res;
            }

            $res['code']=1;
            return $res;
        }

        public static function checkPassword($password,$minLen = 6,$maxLen = 16){

            $match='/^[\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\'\"\d\w]{'.$minLen.','.$maxLen.'}$/';

            $v = trim($password);

            if(empty($v)){
                return false;
            }
            return preg_match($match,$password);
        }


        /**
         * 检查密码强度
         * @param string $pwd
         * @return number
         */
        public static function pwdStrength($pwd){
            $score=0;
            if (strlen($pwd)==0){
                return $score;
            }
            $len=strlen($pwd);
            if (strtolower($pwd)!=$pwd){
                $score++;
            }

            if (strtoupper($pwd)==$pwd){
                $score++;
            }

            if (!is_numeric($pwd)){
                $score++;
            }

            $special_chars=array();
            preg_match_all("/[!@#\$%\*\(\)-_\+\=\\\|\]\}\{\[:;\/\?\.><,'\"]/", $pwd,$special_chars);
            if (count($special_chars[0])>0){
                //echo '[sp1_'.count($special_chars[0]).']';
                $score+=count($special_chars[0]);
            }

            $pwds=str_split($pwd);
            $pwd_chars=array_unique($pwds);
            if (count($pwd_chars)>1){
                $score+=ceil(count($pwd_chars)/2);

                if ($len>=8){
                    $score++;
                }
            }

            $match_num=0;
            if (preg_match('/[0-9]+/',$pwd)){
                $match_num+=1;
            }
            if (preg_match('/[a-z]+/',$pwd)){
                $match_num+=1;
            }
            if (preg_match('/[A-Z]+/',$pwd)){
                $match_num+=1;
            }
            if (preg_match('/[|!@#\$%\^&\*\(\)-_\+\/]+/',$pwd)){
                $match_num+=2;
            }

            if ($match_num==2){
                $score++;
            }
            if ($match_num>=3){
                $score+=$match_num;
            }

            return $score;

        }

        public static function isEmail($mail){
            return preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$mail);
        }

        public static function isMobile($value,$match='/^[(86)|0]?(13\d{9})|(15\d{9})|(17\d{9})|(18\d{9})$/'){
            $v = trim($value);
            if(empty($v)){
                return false;
            }
            return preg_match($match,$v);
        }
        /*
        * @des :验证IP
        * @param : $value string IP值
        * @return : bool
        */
        public static function checkIp($value = ''){
            return (empty($value) ? false : (filter_var($value,FILTER_VALIDATE_IP)));
        }

        public static function isTrueName($trueName = ''){
            return preg_match('/^[\x{4e00}-\x{9fa5}]{2,}$/u',$trueName);
        }
        /*
        * @des : 检测身份证
        * @param : string $vStr 身份证
        * @return : bool
        */
        public static function isIdCard($vStr = ''){

            if(empty($vStr)){
                return false;
            }

            $vCity = array(
                '11','12','13','14','15','21','22',
                '23','31','32','33','34','35','36',
                '37','41','42','43','44','45','46',
                '50','51','52','53','54','61','62',
                '63','64','65','71','81','82','91'
            );

            if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return false;

            if (!in_array(substr($vStr, 0, 2), $vCity)) return false;

            $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
            $vLength = strlen($vStr);

            if ($vLength == 18)
            {
                $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
            } else {
                $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
            }

            if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false;
            if ($vLength == 18)
            {
                $vSum = 0;

                for ($i = 17 ; $i >= 0 ; $i--)
                {
                    $vSubStr = substr($vStr, 17 - $i, 1);
                    $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
                }

                if($vSum % 11 != 1) return false;
            }

            return true;
        }

        /**
         * [isQQ 是否为QQ]
         * @param  [string]  $subject [description]
         * @return boolean      [description]
         */
        public static function isQQ($subject) {
            return preg_match("/[1-9]([0-9]{5,11})/", $subject);
        }

        /**
         * [isLandline 是否为固定电话]
         * @param  [type]  $subject [description]
         * @return boolean          [description]
         */
        public static function isLandline($subject) {
            return preg_match("/[0-9-()()]{7,18}/", $subject);
        }
    }

 

转载于:https://www.cnblogs.com/lonmyblog/p/9396258.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值