• php 获取IP 根据IP 获取城市信息 判断是否手机登陆


    function GetIp(){
    $realip = '';
    $unknown = 'unknown';
    if (isset($_SERVER)){
    if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)){
    $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    foreach($arr as $ip){
    $ip = trim($ip);
    if ($ip != 'unknown'){
    $realip = $ip;
    break;
    }
    }
    }else if(isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP']) && strcasecmp($_SERVER['HTTP_CLIENT_IP'], $unknown)){
    $realip = $_SERVER['HTTP_CLIENT_IP'];
    }else if(isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)){
    $realip = $_SERVER['REMOTE_ADDR'];
    }else{
    $realip = $unknown;
    }
    }else{
    if(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), $unknown)){
    $realip = getenv("HTTP_X_FORWARDED_FOR");
    }else if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), $unknown)){
    $realip = getenv("HTTP_CLIENT_IP");
    }else if(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), $unknown)){
    $realip = getenv("REMOTE_ADDR");
    }else{
    $realip = $unknown;
    }
    }
    $realip = preg_match("/[d.]{7,15}/", $realip, $matches) ? $matches[0] : $unknown;
    return $realip;
    }

    function GetIpLookup($ip = ''){
    if(empty($ip)){
    $ip = GetIp();
    }
    $res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);
    if(empty($res)){ return false; }
    $jsonMatches = array();
    preg_match('#{.+?}#', $res, $jsonMatches);
    if(!isset($jsonMatches[0])){ return false; }
    $json = json_decode($jsonMatches[0], true);
    if(isset($json['ret']) && $json['ret'] == 1){
    $json['ip'] = $ip;
    unset($json['ret']);
    }else{
    return false;
    }
    return $json;
    }

    // 判断是否手机登陆
    function is_mobile() {
    $is_mobile = false;
    if (empty($_SERVER['HTTP_USER_AGENT'])) {
    $is_mobile = false;
    } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false) {
    $is_mobile = true;
    } else {
    $is_mobile = false;
    }

    return $is_mobile;
    }

    //获取用户IP
    $user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
    $user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];

    //获取城市信息
    $ipInfos = GetIpLookup($user_IP);

  • 相关阅读:
    ubuntu上部署ceph集群
    Ceph介绍及原理架构分享
    搭建家庭 NAS 服务器
    Android让屏幕保持常亮,不熄屏的三种方法
    IP流 TS流 PS流之间的关系及区别
    【WPF】Command 自定义命令
    WPF的路由事件、冒泡事件、隧道事件(预览事件)
    WPF之路路由事件
    c# string按指时间格式转化成datetim
    WPF样式(Style)入门
  • 原文地址:https://www.cnblogs.com/QQ42246692/p/4540214.html
Copyright © 2020-2023  润新知