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'));
}
}
}