1.获取访客操作系统信息

  1.  

    <?php
  2.  

    function GetOs() {
  3.  

    if (!empty($_SERVER['HTTP_USER_AGENT'])) {
  4.  

    $OS = $_SERVER['HTTP_USER_AGENT'];
  5.  

    if (preg_match('/win/i', $OS)) {
  6.  

    $OS = 'Windows';
  7.  

    } elseif (preg_match('/mac/i', $OS)) {
  8.  

    $OS = 'MAC';
  9.  

    } elseif (preg_match('/linux/i', $OS)) {
  10.  

    $OS = 'Linux';
  11.  

    } elseif (preg_match('/unix/i', $OS)) {
  12.  

    $OS = 'Unix';
  13.  

    } elseif (preg_match('/bsd/i', $OS)) {
  14.  

    $OS = 'BSD';
  15.  

    } else {
  16.  

    $OS = 'Other';
  17.  

    }
  18.  

    return $OS;
  19.  

    } else {
  20.  

    return "获取访客操作系统信息失败!";
  21.  

    }
  22.  

    } ?>

2.获取浏览器信息

  1.  

    <?php
  2.  

    function GetBrowser() {
  3.  

    if (!empty($_SERVER['HTTP_USER_AGENT'])) {
  4.  

    $br = $_SERVER['HTTP_USER_AGENT'];
  5.  

    if (preg_match('/MSIE/i', $br)) {
  6.  

    $br = 'MSIE';
  7.  

    } elseif (preg_match('/Firefox/i', $br)) {
  8.  

    $br = 'Firefox';
  9.  

    } elseif (preg_match('/Chrome/i', $br)) {
  10.  

    $br = 'Chrome';
  11.  

    } elseif (preg_match('/Safari/i', $br)) {
  12.  

    $br = 'Safari';
  13.  

    } elseif (preg_match('/Opera/i', $br)) {
  14.  

    $br = 'Opera';
  15.  

    } else {
  16.  

    $br = 'Other';
  17.  

    }
  18.  

    return $br;
  19.  

    } else {
  20.  

    return "获取浏览器信息失败!";
  21.  

    }
  22.  

    } ?>

 

3.获取浏览器语言

  1.  

    <?php
  2.  

    function GetLang() {
  3.  

    if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  4.  

    $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  5.  

    $lang = substr($lang, 0, 5);
  6.  

    if (preg_match("/zh-cn/i", $lang)) {
  7.  

    $lang = "简体中文";
  8.  

    } elseif (preg_match("/zh/i", $lang)) {
  9.  

    $lang = "繁体中文";
  10.  

    } else {
  11.  

    $lang = "English";
  12.  

    }
  13.  

    return $lang;
  14.  

    } else {
  15.  

    return "获取浏览器语言失败!";
  16.  

    }
  17.  

    } ?>

4.获取ip和地址信息


ip地址信息由新浪ip库提供


  1.  

    <?php
  2.  

    //获取ip
  3.  

    function getip() {
  4.  

    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP") , "unknown")) {
  5.  

    $ip = getenv("HTTP_CLIENT_IP");
  6.  

    } else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR") , "unknown")) {
  7.  

    $ip = getenv("HTTP_X_FORWARDED_FOR");
  8.  

    } else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR") , "unknown")) {
  9.  

    $ip = getenv("REMOTE_ADDR");
  10.  

    } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
  11.  

    $ip = $_SERVER['REMOTE_ADDR'];
  12.  

    } else {
  13.  

    $ip = "unknown";
  14.  

    }
  15.  

    return $ip;
  16.  

    }
  17.  

    //获取地址
  18.  

    function getLocation($ip = '') {
  19.  

    empty($ip) && $ip = getip();
  20.  

    if ($ip == "127.0.0.1") return "本机地址";
  21.  

    $api = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip"; //请求新浪ip地址库
  22.  

    $json = @file_get_contents($api);
  23.  

    $arr = json_decode($json, true);
  24.  

    $country = $arr['country'];
  25.  

    $province = $arr['province'];
  26.  

    $city = $arr['city'];
  27.  

    if ((string)$country == "中国") {
  28.  

    if ((string)($province) != (string)$city) {
  29.  

    $_location = $province . $city;
  30.  

    } else {
  31.  

    $_location = $country . $city;
  32.  

    }
  33.  

    } else {
  34.  

    $_location = $country;
  35.  

    }
  36.  

    return $_location;
  37.  

    } ?>

i> 使用时直接在需要输出的地方调用函数即可 例如:​​<?php echo getip();?>​