PHP获取访客ip系统浏览器等信息[转]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP获取访客ip系统浏览器等信息[转]相关的知识,希望对你有一定的参考价值。
1.获取访客操作系统信息
-
-
function GetOs() {
-
if (!empty($_SERVER[‘HTTP_USER_AGENT‘])) {
-
$OS = $_SERVER[‘HTTP_USER_AGENT‘];
-
if (preg_match(‘/win/i‘, $OS)) {
-
$OS = ‘Windows‘;
-
} elseif (preg_match(‘/mac/i‘, $OS)) {
-
$OS = ‘MAC‘;
-
} elseif (preg_match(‘/linux/i‘, $OS)) {
-
$OS = ‘Linux‘;
-
} elseif (preg_match(‘/unix/i‘, $OS)) {
-
$OS = ‘Unix‘;
-
} elseif (preg_match(‘/bsd/i‘, $OS)) {
-
$OS = ‘BSD‘;
-
} else {
-
$OS = ‘Other‘;
-
}
-
return $OS;
-
} else {
-
return "获取访客操作系统信息失败!";
-
}
-
}
2.获取浏览器信息
-
-
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 "获取浏览器信息失败!";
-
}
-
}
3.获取浏览器语言
-
-
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 "获取浏览器语言失败!";
-
}
-
}
4.获取ip和地址信息
ip地址信息由新浪ip库提供
-
-
//获取ip
-
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 = ‘‘) {
-
empty($ip) && $ip = getip();
-
if ($ip == "127.0.0.1") return "本机地址";
-
$api = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip"; //请求新浪ip地址库
-
$json = @file_get_contents($api);
-
$arr = json_decode($json, true);
-
$country = $arr[‘country‘];
-
$province = $arr[‘province‘];
-
$city = $arr[‘city‘];
-
if ((string)$country == "中国") {
-
if ((string)($province) != (string)$city) {
-
$_location = $province . $city;
-
} else {
-
$_location = $country . $city;
-
}
-
} else {
-
$_location = $country;
-
}
-
return $_location;
-
}
i> 使用时直接在需要输出的地方调用函数即可 例如:<?php echo getip();?>
原文: https://blog.csdn.net/MaXSSR/article/details/80886258
以上是关于PHP获取访客ip系统浏览器等信息[转]的主要内容,如果未能解决你的问题,请参考以下文章