当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP制作登录异常ip检测功能的实例代码

PHP制作登录异常ip检测功能的实例代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

使用函数查询数据库遍历实现

/**
 * 不在常用ip地址登录返回描红信息
 * @param string $ip  ip地址
 * @param string $name  用户名
 * @return string
 */

function errorip($ip,$name){

  $nowip = get_client_ip();
  //判断ip和当前ip是否相同,不同则查询数据库对比
  if($ip == $nowip ){
    //相同直接返回字符串
    $str = '<font color="blue"'.">登录ip:".$ip."</font>";
  }else{
    //不同则记数这个ip地址数量
    $count =  m('log')->where("name='{$name}' and ip='{$ip}'")->count();
    //如果超过一定数量则是正常ip否则为异常返回字符串
    if($count > 10){
      $str = '<font color="blue"'.">登录ip:".$ip."</font>";
    }else{
      $str = '<font color="red"'.">异常ip:".$ip."</font>";
    }
  }

  return $str;

}

注释:适合所有框架使用,get_client_ip()是ip获取函数。

get_client_ip函数片段:

function get_client_ip() {
  if(getenv('http_client_ip') && strcasecmp(getenv('http_client_ip'), 'unknown')) {
    $ip = getenv('http_client_ip');
  } elseif(getenv('http_x_forwarded_for') && strcasecmp(getenv('http_x_forwarded_for'), 'unknown')) {
    $ip = getenv('http_x_forwarded_for');
  } elseif(getenv('remote_addr') && strcasecmp(getenv('remote_addr'), 'unknown')) {
    $ip = getenv('remote_addr');
  } else{
    $ip = $_server['remote_addr'];
  }
  return $ip;
}

以上这篇php制作登录异常ip检测功能的实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网