获取客户端浏览器、IP地址操作系统类型等的方法(PHP)
=========================
获取客户端信息(浏览器、IP、OS)
/*
作用:取得客户端信息
参数:
返回:指定的资料
使用:
$code = new clientGetObj;
1、浏览器:$str = $code->getBrowse();
2、IP地址:$str = $code->getIP();
4、操作系统:$str = $code->getOS();
*/
class clientGetObj
{
function getBrowse()
{
global $_SERVER;
$Agent = $_SERVER['HTTP_USER_AGENT'];
$browser = '';
$browserver = '';
$Browser = array('Lynx', 'MOSAIC', 'AOL', 'Opera', 'JAVA', 'MacWeb', 'WebExplorer', 'OmniWeb');
for($i = 0; $i <= 7; $i ++){
if(strpos($Agent, $Browsers[$i])){
$browser = $Browsers[$i];
$browserver = '';
}
}
if(ereg('Mozilla', $Agent) && !ereg('MSIE', $Agent)){
$temp = explode('(', $Agent);
$Part = $temp[0];
$temp = explode('/', $Part);
$browserver = $temp[1];
$temp = explode(' ', $browserver);
$browserver = $temp[0];
$browserver = preg_replace('/([d.]+)/', '\1', $browserver);
$browserver = $browserver;
$browser = 'Netscape Navigator';
}
if(ereg('Mozilla', $Agent) && ereg('Opera', $Agent)) {
$temp = explode('(', $Agent);
$Part = $temp[1];
$temp = explode(')', $Part);
$browserver = $temp[1];
$temp = explode(' ', $browserver);
$browserver = $temp[2];
$browserver = preg_replace('/([d.]+)/', '\1', $browserver);
$browserver = $browserver;
$browser = 'Opera';
}
if(ereg('Mozilla', $Agent) && ereg('MSIE', $Agent)){
$temp = explode('(', $Agent);
$Part = $temp[1];
$temp = explode(';', $Part);
$Part = $temp[1];
$temp = explode(' ', $Part);
$browserver = $temp[2];
$browserver = preg_replace('/([d.]+)/','\1',$browserver);
$browserver = $browserver;
$browser = 'Internet Explorer';
}
if($browser != ''){
$browseinfo = $browser.' '.$browserver;
} else {
$browseinfo = false;
}
return $browseinfo;
}
function getIP ()
{
global $_SERVER;
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
} else if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} else if (getenv('REMOTE_ADDR')) {
$ip = getenv('REMOTE_ADDR');
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function getOS ()
{
global $_SERVER;
$agent = $_SERVER['HTTP_USER_AGENT'];
$os = false;
if (eregi('win', $agent) && strpos($agent, '95')){
$os = 'Windows 95';
}
else if (eregi('win 9x', $agent) && strpos($agent, '4.90')){
$os = 'Windows ME';
}
else if (eregi('win', $agent) && ereg('98', $agent)){
$os = 'Windows 98';
}
else if (eregi('win', $agent) && eregi('nt 5.1', $agent)){
$os = 'Windows XP';
}
else if (eregi('win', $agent) && eregi('nt 5', $agent)){
$os = 'Windows 2000';
}
else if (eregi('win', $agent) && eregi('nt', $agent)){
$os = 'Windows NT';
}
else if (eregi('win', $agent) && ereg('32', $agent)){
$os = 'Windows 32';
}
else if (eregi('linux', $agent)){
$os = 'Linux';
}
else if (eregi('unix', $agent)){
$os = 'Unix';
}
else if (eregi('sun', $agent) && eregi('os', $agent)){
$os = 'SunOS';
}
else if (eregi('ibm', $agent) && eregi('os', $agent)){
$os = 'IBM OS/2';
}
else if (eregi('Mac', $agent) && eregi('PC', $agent)){
$os = 'Macintosh';
}
else if (eregi('PowerPC', $agent)){
$os = 'PowerPC';
}
else if (eregi('AIX', $agent)){
$os = 'AIX';
}
else if (eregi('HPUX', $agent)){
$os = 'HPUX';
}
else if (eregi('NetBSD', $agent)){
$os = 'NetBSD';
}
else if (eregi('BSD', $agent)){
$os = 'BSD';
}
else if (ereg('OSF1', $agent)){
$os = 'OSF1';
}
else if (ereg('IRIX', $agent)){
$os = 'IRIX';
}
else if (eregi('FreeBSD', $agent)){
$os = 'FreeBSD';
}
else if (eregi('teleport', $agent)){
$os = 'teleport';
}
else if (eregi('flashget', $agent)){
$os = 'flashget';
}
else if (eregi('webzip', $agent)){
$os = 'webzip';
}
else if (eregi('offline', $agent)){
$os = 'offline';
}
else {
$os = 'Unknown';
}
return $os;
}
}
?>
获取IP地址
说明:使用 Request.ServerVariables("REMOTE_ADDR") 来取得客户端的 IP 地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的 IP 地址,而不是真正的客户端 IP 地址。要想透过代理服务器取得客户端的真实IP 地址,就要使用Request.ServerVariables("HTTP_X_FORWARDED_FOR") 来读取。不过要注意的事,并不是每个代理服务器都能用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 来读取客户端的真实 IP,有些用此方法读取到的仍然是代理服务器的 IP。
还有一点需要注意的是:如果客户端没有通过代理服务器来访问,那么用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 取到的值将是空的。因此,如果要在程序中使用此方法,可以这样处理:
......
userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If userip = "" Then userip = Request.ServerVariables("REMOTE_ADDR")
......
即:如果客户端通过代理服务器,则取 HTTP_X_FORWARDED_FOR 的值,如果没通过代理服务器,就取 REMOTE_ADDR 的值。
方法一:
function real_ip($type)
{
if ($_SERVER["HTTP_X_FORWARDED_FOR"])
{
if ($_SERVER["HTTP_CLIENT_IP"])
{
$proxy = $_SERVER["HTTP_CLIENT_IP"];
}
else
{
$proxy = $_SERVER["REMOTE_ADDR"];
}
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else
{
if ($_SERVER["HTTP_CLIENT_IP"])
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
else
{
$ip = $_SERVER["REMOTE_ADDR"];
}
}
if($type=='ip')return $ip;
if($type=='proxy')return $proxy;
}
方法二:
function iptype1 () {
if (getenv("HTTP_CLIENT_IP")) {
?return getenv("HTTP_CLIENT_IP");
}
else {
?return "none";
}
}
function iptype2 () {
if (getenv("HTTP_X_FORWARDED_FOR")) {
?return getenv("HTTP_X_FORWARDED_FOR");
}
else {
?return "none";
}
}
function iptype3 () {
if (getenv("REMOTE_ADDR")) {
?return getenv("REMOTE_ADDR");
}
else {
?return "none";
}
}
function ip() {
$ip1 = iptype1();
$ip2 = iptype2();
$ip3 = iptype3();
if (isset($ip1) && $ip1 != "none" && $ip1 != "unknown") {
?return $ip1;
}
elseif (isset($ip2) && $ip2 != "none" && $ip2 != "unknown") {
?return $ip2;
}
elseif (isset($ip3) && $ip3 != "none" && $ip3 != "unknown") {
?return $ip3;
}?
else {
return "none";
}
}
Echo ip();
?>
方法三:
01 : function ipCheck() {
02 : if (getenv( 'HTTP_CLIENT_IP' )) {
03 : $ip = getenv( 'HTTP_CLIENT_IP' );
04 : }
05 : elseif (getenv( 'HTTP_X_FORWARDED_FOR' )) {
06 : $ip = getenv( 'HTTP_X_FORWARDED_FOR' );
07 : }
08 : elseif (getenv( 'HTTP_X_FORWARDED' )) {
09 : $ip = getenv( 'HTTP_X_FORWARDED' );
10 : }
11 : elseif (getenv( 'HTTP_FORWARDED_FOR' )) {
12 : $ip = getenv( 'HTTP_FORWARDED_FOR' );
13 : }
14 : elseif (getenv( 'HTTP_FORWARDED' )) {
15 : $ip = getenv( 'HTTP_FORWARDED' );
16 : }
17 : else {
18 : $ip = $_SERVER[ 'REMOTE_ADDR' ];
19 : }
20 : return $ip;
21 : }
function getBrowser(){
$agent=$_SERVER["HTTP_USER_AGENT"];
if(strpos($agent,'MSIE')!==false || strpos($agent,'rv:11.0')) //ie11判断
return "ie";
else if(strpos($agent,'Firefox')!==false)
return "firefox";
else if(strpos($agent,'Chrome')!==false)
return "chrome";
else if(strpos($agent,'Opera')!==false)
return 'opera';
else if((strpos($agent,'Chrome')==false)&&strpos($agent,'Safari')!==false)
return 'safari';
else
return 'unknown';
}
function getBrowserVer(){
if (empty($_SERVER['HTTP_USER_AGENT'])){ //当浏览器没有发送访问者的信息的时候
return 'unknow';
}
$agent= $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/MSIE\s(\d+)\..*/i', $agent, $regs))
return $regs[1];
elseif (preg_match('/FireFox\/(\d+)\..*/i', $agent, $regs))
return $regs[1];
elseif (preg_match('/Opera[\s|\/](\d+)\..*/i', $agent, $regs))
return $regs[1];
elseif (preg_match('/Chrome\/(\d+)\..*/i', $agent, $regs))
return $regs[1];
elseif ((strpos($agent,'Chrome')==false)&&preg_match('/Safari\/(\d+)\..*$/i', $agent, $regs))
return $regs[1];
else
return 'unknow';
}
print_r(getBrowser());
print_r(getBrowserVer());
echo '
'.$_SERVER["HTTP_USER_AGENT"];
可以使用预定义变量:
$_SERVER['HTTP_USER_AGENT']
返回如下:
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7