菜鸟写的工具类--验证篇(thinkphp)

ThinkPHP5简单实现状态和跳转检测

检测ip是否在文件中

	public function queryIpFile($ip, $lx = 0)
    {
        $dir = "./public/ip.txt";//文件路径
        $filedata = file_get_contents($dir);//打开文件
        $newfiledata = strstr($filedata, $ip);//查找
        if ($newfiledata) {//判断文件是否存在
            return 1;
        }
        if ($lx == 1) {//紧急类型
            if (!$newfiledata) {//判断文件是否存在 不存在才需要插入
                $ip_string = $ip . ',';//拼接字符串
                file_put_contents($dir, $ip_string, FILE_APPEND);//插入数到文件中
            }
            return 1;
        }
        return 0;//非紧急类型跳出
    }

需要查询ip时

$this->queryIpFile($ip);

需要插入ip时

$this->queryIpFile($ip,1);//1表示紧急类型

配合使用例子

 	//跳转校验
    public function openUrl($url,$cdx=0)
    {
        $request = Request::instance();
        $ip = $request->ip();
        $ipnum = $this->queryIpFile($ip);
        if ($ipnum == 1) {
            return $this->Massage("你已被系统限制访问!误判请联系管理员!", '/url', 'err');//封装的方法
        }
        if($cdx==1){
            return $this->redirect($url);//重定向
        }
        return $this->fetch($url);//跳转模板文件
    }
	//访问校验
    public function dologin()
    {
        $request = Request::instance();
        $ip = $request->ip();
        $login = Session::get('user');
        if (!$login) {
            return $this->error('请先登录!', url('/url'));
        } else {//如果是的话查询数据库是否存在
            $datas = array(
                '*Acc' => $login['*_acc'],
                '*Pwd' => $login['*_pwd'],
                '*key' => $login['*_key']
            );
            $res = $this->model方法($datas);
            if (!$res) {
                $this->queryIpFile($ip, 1);
                return $this->error('非法访问!', url('/url'));
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值