当前位置:首页 > PHP教程 > php应用 > 列表

PHP 记录访客的浏览信息方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-09-01 16:04:14 浏览: 评论:0 

下面小编就为大家分享一篇PHP 记录访客的浏览信息方法,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧。

可以记录访客的地理位置,操作系统,浏览器,IP,时间和访问的文件。

1.首先创建一个comm_function.php文件:

  1. <?php 
  2. //echo $_SERVER['HTTP_USER_AGENT']; 
  3. //echo "<br />".$_SERVER ['REMOTE_ADDR']; 
  4. /** 
  5.  * 获取客户端类型,手机还是电脑,以及相应的操作系统类型。 
  6.  * 
  7.  * @param string $subject 
  8.  */ 
  9. function get_os($agent) { 
  10.   $os = false; 
  11.   if (preg_match ( '/win/i', $agent ) && strpos ( $agent, '95' )) { 
  12.     $os = 'Windows 95'; 
  13.   } else if (preg_match ( '/win 9x/i', $agent ) && strpos ( $agent, '4.90' )) { 
  14.     $os = 'Windows ME'; 
  15.   } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/98/i', $agent )) { 
  16.     $os = 'Windows 98'; 
  17.   } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 6.0/i', $agent )) { 
  18.     $os = 'Windows Vista'; 
  19.   } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 6.1/i', $agent )) { 
  20.     $os = 'Windows 7'; 
  21.   } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 6.2/i', $agent )) { 
  22.     $os = 'Windows 8'; 
  23.   } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 10.0/i', $agent )) { 
  24.     $os = 'Windows 10'; // 添加win10判断 
  25.   } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 5.1/i', $agent )) { 
  26.     $os = 'Windows XP'; 
  27.   } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 5/i', $agent )) { 
  28.     $os = 'Windows 2000'; 
  29.   } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt/i', $agent )) { 
  30.     $os = 'Windows NT'; 
  31.   } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/32/i', $agent )) { 
  32.     $os = 'Windows 32'; 
  33.   } else if (preg_match ( '/linux/i', $agent )) { 
  34.     if(preg_match("/Mobile/", $agent)){ 
  35.       if(preg_match("/QQ/i", $agent)){ 
  36.         $os = "Android QQ Browser"; 
  37.       }else{ 
  38.         $os = "Android Browser"; 
  39.       } 
  40.     }else{ 
  41.       $os = 'PC-Linux'; 
  42.     } 
  43.   } else if (preg_match ( '/Mac/i', $agent )) { 
  44.     if(preg_match("/Mobile/", $agent)){ 
  45.       if(preg_match("/QQ/i", $agent)){ 
  46.         $os = "IPhone QQ Browser"; 
  47.       }else{ 
  48.         $os = "IPhone Browser"; 
  49.       } 
  50.     }else{ 
  51.       $os = 'Mac OS X'; 
  52.     } 
  53.   } else if (preg_match ( '/unix/i', $agent )) { 
  54.     $os = 'Unix'; 
  55.   } else if (preg_match ( '/sun/i', $agent ) && preg_match ( '/os/i', $agent )) { 
  56.     $os = 'SunOS'; 
  57.   } else if (preg_match ( '/ibm/i', $agent ) && preg_match ( '/os/i', $agent )) { 
  58.     $os = 'IBM OS/2'; 
  59.   } else if (preg_match ( '/Mac/i', $agent ) && preg_match ( '/PC/i', $agent )) { 
  60.     $os = 'Macintosh'; 
  61.   } else if (preg_match ( '/PowerPC/i', $agent )) { 
  62.     $os = 'PowerPC'; 
  63.   } else if (preg_match ( '/AIX/i', $agent )) { 
  64.     $os = 'AIX'; 
  65.   } else if (preg_match ( '/HPUX/i', $agent )) { 
  66.     $os = 'HPUX'; 
  67.   } else if (preg_match ( '/NetBSD/i', $agent )) { 
  68.     $os = 'NetBSD'; 
  69.   } else if (preg_match ( '/BSD/i', $agent )) { 
  70.     $os = 'BSD'; 
  71.   } else if (preg_match ( '/OSF1/i', $agent )) { 
  72.     $os = 'OSF1'; 
  73.   } else if (preg_match ( '/IRIX/i', $agent )) { 
  74.     $os = 'IRIX'; 
  75.   } else if (preg_match ( '/FreeBSD/i', $agent )) { 
  76.     $os = 'FreeBSD'; 
  77.   } else if (preg_match ( '/teleport/i', $agent )) { 
  78.     $os = 'teleport'; 
  79.   } else if (preg_match ( '/flashget/i', $agent )) { 
  80.     $os = 'flashget'; 
  81.   } else if (preg_match ( '/webzip/i', $agent )) { 
  82.     $os = 'webzip'; 
  83.   } else if (preg_match ( '/offline/i', $agent )) { 
  84.     $os = 'offline'; 
  85.   } else { 
  86.     $os = '未知操作系统'; 
  87.   } 
  88.   return $os; 
  89. } 
  90. /** 
  91.  * 获取 客户端的浏览器类型 
  92.  * @return string 
  93.  */ 
  94. function get_broswer($sys){ 
  95.   if (stripos($sys, "Firefox/") > 0) { 
  96.     preg_match("/Firefox\/([^;)]+)+/i", $sys, $b); 
  97.     $exp[0] = "Firefox"; 
  98.     $exp[1] = $b[1]; //获取火狐浏览器的版本号 
  99.   } elseif (stripos($sys, "Maxthon") > 0) { 
  100.     preg_match("/Maxthon\/([\d\.]+)/", $sys, $aoyou); 
  101.     $exp[0] = "傲游"; 
  102.     $exp[1] = $aoyou[1]; 
  103.   } elseif (stripos($sys, "MSIE") > 0) { 
  104.     preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie); 
  105.     $exp[0] = "IE"; 
  106.     $exp[1] = $ie[1]; //获取IE的版本号 
  107.   } elseif (stripos($sys, "OPR") > 0) { 
  108.     preg_match("/OPR\/([\d\.]+)/", $sys, $opera); 
  109.     $exp[0] = "Opera"; 
  110.     $exp[1] = $opera[1]; 
  111.   } elseif(stripos($sys, "Edge") > 0) { 
  112.     //win10 Edge浏览器 添加了chrome内核标记 在判断Chrome之前匹配 
  113.     preg_match("/Edge\/([\d\.]+)/", $sys, $Edge); 
  114.     $exp[0] = "Edge"; 
  115.     $exp[1] = $Edge[1]; 
  116.   } elseif (stripos($sys, "Chrome") > 0) { 
  117.     preg_match("/Chrome\/([\d\.]+)/", $sys, $google); 
  118.     $exp[0] = "Chrome"; 
  119.     $exp[1] = $google[1]; //获取google chrome的版本号 
  120.   } elseif(stripos($sys,'rv:')>0 && stripos($sys,'Gecko')>0){ 
  121.     preg_match("/rv:([\d\.]+)/", $sys, $IE); 
  122.     $exp[0] = "IE"; 
  123.     $exp[1] = $IE[1]; 
  124.   }else { 
  125.     $exp[0] = "未知浏览器"; 
  126.     $exp[1] = ""; 
  127.   } 
  128.   return $exp[0].'('.$exp[1].')'; 
  129. } 
  130. /** 
  131.  * 根据 客户端IP 获取到其具体的位置信息 
  132.  * @param unknown $ip 
  133.  * @return string 
  134.  */ 
  135. function get_address_by_ip($ip) { 
  136.   $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; 
  137.   $curl = curl_init(); 
  138.   curl_setopt($curl, CURLOPT_URL, $url); 
  139.   curl_setopt($curl, CURLOPT_HEADER, 0); 
  140.   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  141.   $info = curl_exec($curl); 
  142.   curl_close($curl); 
  143.   return $info; 
  144. } 
  145. function clientlog() { 
  146.   $useragent = $_SERVER ['HTTP_USER_AGENT']; 
  147.   $clientip = $_SERVER ['REMOTE_ADDR']; 
  148.   $client_info = get_os ( $useragent ) . "---" . get_broswer ( $useragent ); 
  149.   $rawdata_position = get_address_by_ip ( $clientip ); 
  150.   $rawdata_position = json_decode($rawdata_position, true); 
  151.   $country = $rawdata_position['data']['country']; 
  152.   $province = $rawdata_position['data']['region']; 
  153.   $city = $rawdata_position['data']['city']; 
  154.   $nettype = $rawdata_position['data']['isp']; 
  155.  
  156.   $time = date ( 'y-m-d h:m:s' ); 
  157.   $data = "来自{$country} {$province} {$city }{$nettype} 的客户端: {$client_info},IP为:{$clientip},在{$time}时刻访问了{$_SERVER['PHP_SELF']}文件!\r\n"; 
  158.   $filename = "./log.log"; 
  159.   if (! file_exists ( $filename )) { 
  160.     fopen ( $filename, "w+" ); 
  161.   } 
  162.   file_put_contents ( $filename, $data, FILE_APPEND ); 
  163. } 

2.在别的文件引入这个comm_function.php

require_once "comm_function.php";

Tags: PHP浏览信息

分享到: