PHP获取用户IP、浏览器、系统语言、IP归属地等信息

发布于 2019-12-23  5 次阅读


<?php
/**
 * Created by PhpStorm.
 * User: tg
 * Date: 2019/2/23
 * Time: 下午4:39
 */
//获取浏览器数据
//获取用户操作系统
function GetOs() {

    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    if (strpos($agent, 'windows nt')) {
        $platform = 'windows';
    } elseif (strpos($agent, 'macintosh')) {
        $platform = 'macOS';
    } elseif (strpos($agent, 'ipod')) {
        $platform = 'ipod';
    } elseif (strpos($agent, 'ipad')) {
        $platform = 'ipad';
    } elseif (strpos($agent, 'iphone')) {
        $platform = 'iphone';
    } elseif (strpos($agent, 'android')) {
        $platform = 'android';
    } elseif (strpos($agent, 'unix')) {
        $platform = 'unix';
    } elseif (strpos($agent, 'linux')) {
        $platform = 'linux';
    } else {
        $platform = 'other';
    }

    return $platform;
}
function GetBrowser() {
    if (!empty($_SERVER['HTTP_USER_AGENT'])) {
        $br = $_SERVER['HTTP_USER_AGENT'];
        if (preg_match('/MSIE/i', $br)) {
            $br = 'MSIE';
        } elseif (preg_match('/Firefox/i', $br)) {
            $br = 'Firefox';
        } elseif (preg_match('/Chrome/i', $br)) {
            $br = 'Chrome';
        } elseif (preg_match('/Safari/i', $br)) {
            $br = 'Safari';
        } elseif (preg_match('/Opera/i', $br)) {
            $br = 'Opera';
        } else {
            $br = 'Other';
        }
        return $br;
    } else {
        return "null";
    }
}

function GetLang() {
    if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
        $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        $lang = substr($lang, 0, 5);
        if (preg_match("/zh-cn/i", $lang)) {
            $lang = "简体中文";
        } elseif (preg_match("/zh/i", $lang)) {
            $lang = "繁体中文";
        } else {
            $lang = "English";
        }
        return $lang;
    } else {
        return "null";
    }
}

function GetIp() {
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
        $ip = getenv("HTTP_CLIENT_IP");
    } else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    } else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
        $ip = getenv("REMOTE_ADDR");
    } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
        $ip = $_SERVER['REMOTE_ADDR'];
    } else {
        $ip = "unknown";
    }
    return $ip;
}

//获取地址
function GetLocation($ip) {
    //设置超时时间
    $opts = [
        'http' => [
            'timeout' => 2
        ]
    ];
    $context = stream_context_create($opts);

    $api = "http://ip.taobao.com/service/getIpInfo.php?ip=$ip";   //请求淘宝ip地址库
    $json = @file_get_contents($api, 0, $context);
    if (!$json){
        return false;
    }
    $arr = json_decode($json, true);

    return $arr['data'];
}

function GetGuest() {
    $ip = GetIp();
    $location = GetLocation($ip);
    if (!$location){
        return false;
    }
    $data = [
        'os' => GetOs(),
        'browser' => GetBrowser(),
        'lang' => GetLang(),
        'ip' => $ip,
        'country' => $location['country'],
        'region' => $location['region'],
        'city' => $location['city'],
        'isp' => $location['isp']
    ];
    if ($data['os'] == 'other'){
        return false;
    }
    return $data;
}

一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。