PHP获取访问设备信息的方法示例

这篇文章主要介绍了PHP获取访问设备信息的方法,结合实例形式分析了php针对访问设备的浏览器类型、浏览器语言、操作系统类型、访客IP、访客地址等相关函数封装与使用技巧,需要的朋友可以参考下。

本文实例讲述了PHP获取访问设备信息的方法,分享给大家供大家参考,具体如下:

  1. <?php
  2. header("Content:Content-type:text/html;charset=utf-8");
  3. // // 作用取得客户端的ip、地理位置、浏览器、以及访问设备
  4. class get_equipment_info{
  5. ////获得访客浏览器类型
  6. function GetBrowser(){
  7. if(!emptyempty($_SERVER['HTTP_USER_AGENT']))
  8. {
  9. $br = $_SERVER['HTTP_USER_AGENT'];
  10. if (preg_match('/MSIE/i',$br)){
  11. $br = 'MSIE';
  12. }
  13. elseif (preg_match('/Firefox/i',$br)){
  14. $br = 'Firefox';
  15. }elseif (preg_match('/Chrome/i',$br)){
  16. $br = 'Chrome';
  17. }elseif (preg_match('/Safari/i',$br)){
  18. $br = 'Safari';
  19. }elseif (preg_match('/Opera/i',$br)){
  20. $br = 'Opera';
  21. }else {
  22. $br = 'Other';
  23. }
  24. return json_encode("浏览器为".$br);
  25. }else{
  26. return "获取浏览器信息失败!";}
  27. }
  28. ////获得访客浏览器语言
  29. function GetLang()
  30. {
  31. if(!emptyempty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
  32. $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  33. $lang = substr($lang,0,5);
  34. if(preg_match("/zh-cn/i",$lang)){
  35. $lang = "简体中文";
  36. }elseif(preg_match("/zh/i",$lang)){
  37. $lang = "繁体中文";
  38. }else{
  39. $lang = "English";
  40. }
  41. return json_encode("浏览器语言为".$lang);
  42. }else{
  43. return "获取浏览器语言失败!";
  44. }
  45. }
  46. //获取客户端操作系统信息包括win10
  47. function GetOs(){
  48. $agent = $_SERVER['HTTP_USER_AGENT'];
  49. $os = false;
  50. if (preg_match('/win/i', $agent) && strpos($agent, '95'))
  51. {
  52. $os = 'Windows 95';
  53. }
  54. else if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90'))
  55. {
  56. $os = 'Windows ME';
  57. }
  58. else if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent))
  59. {
  60. $os = 'Windows 98';
  61. }
  62. else if (preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent))
  63. {
  64. $os = 'Windows Vista';
  65. }
  66. else if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent))
  67. {
  68. $os = 'Windows 7';
  69. }
  70. else if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent))
  71. {
  72. $os = 'Windows 8';
  73. }else if(preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent))
  74. {
  75. $os = 'Windows 10';#添加win10判断
  76. }else if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent))
  77. {
  78. $os = 'Windows XP';
  79. }
  80. else if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent))
  81. {
  82. $os = 'Windows 2000';
  83. }
  84. else if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent))
  85. {
  86. $os = 'Windows NT';
  87. }
  88. else if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent))
  89. {
  90. $os = 'Windows 32';
  91. }
  92. else if (preg_match('/linux/i', $agent))
  93. {
  94. $os = 'Linux';
  95. }
  96. else if (preg_match('/unix/i', $agent))
  97. {
  98. $os = 'Unix';
  99. }
  100. else if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent))
  101. {
  102. $os = 'SunOS';
  103. }
  104. else if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent))
  105. {
  106. $os = 'IBM OS/2';
  107. }
  108. else if (preg_match('/Mac/i', $agent) && preg_match('/PC/i', $agent))
  109. {
  110. $os = 'Macintosh';
  111. }
  112. else if (preg_match('/PowerPC/i', $agent))
  113. {
  114. $os = 'PowerPC';
  115. }
  116. else if (preg_match('/AIX/i', $agent))
  117. {
  118. $os = 'AIX';
  119. }
  120. else if (preg_match('/HPUX/i', $agent))
  121. {
  122. $os = 'HPUX';
  123. }
  124. else if (preg_match('/NetBSD/i', $agent))
  125. {
  126. $os = 'NetBSD';
  127. }
  128. else if (preg_match('/BSD/i', $agent))
  129. {
  130. $os = 'BSD';
  131. }
  132. else if (preg_match('/OSF1/i', $agent))
  133. {
  134. $os = 'OSF1';
  135. }
  136. else if (preg_match('/IRIX/i', $agent))
  137. {
  138. $os = 'IRIX';
  139. }
  140. else if (preg_match('/FreeBSD/i', $agent))
  141. {
  142. $os = 'FreeBSD';
  143. }
  144. else if (preg_match('/teleport/i', $agent))
  145. {
  146. $os = 'teleport';
  147. }
  148. else if (preg_match('/flashget/i', $agent))
  149. {
  150. $os = 'flashget';
  151. }
  152. else if (preg_match('/webzip/i', $agent))
  153. {
  154. $os = 'webzip';
  155. }
  156. else if (preg_match('/offline/i', $agent))
  157. {
  158. $os = 'offline';
  159. }
  160. else
  161. {
  162. $os = '未知操作系统';
  163. }
  164. return json_encode("系统为".$os);
  165. }
  166. //获得访客真实ip
  167. function Getip()
  168. {
  169. if (! emptyempty($_SERVER["HTTP_CLIENT_IP"])) {
  170. $ip = $_SERVER["HTTP_CLIENT_IP"];
  171. }
  172. if (! emptyempty($_SERVER['HTTP_X_FORWARDED_FOR'])) { // 获取代理ip
  173. $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  174. }
  175. if ($ip) {
  176. $ips = array_unshift($ips, $ip);
  177. }
  178. $count = count($ips);
  179. for ($i = 0; $i < $count; $i ++) {
  180. if (! preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i])) { // 排除局域网ip
  181. $ip = $ips[$i];
  182. break;
  183. }
  184. }
  185. $tip = emptyempty($_SERVER['REMOTE_ADDR']) ? $ip : $_SERVER['REMOTE_ADDR'];
  186. if ($tip == "127.0.0.1") { // 获得本地真实IP
  187. return $this->get_onlineip();
  188. } else {
  189. return $tip;
  190. }
  191. }
  192. // //根据ip获得访客所在地地名
  193. function Getaddress($ip = '')
  194. {
  195. if (emptyempty($ip)) {
  196. $ip = $this->Getip();
  197. }
  198. $ipadd = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" . $ip); // 根据新浪api接口获取
  199. if ($ipadd) {
  200. $charset = iconv("gbk", "utf-8", $ipadd);
  201. preg_match_all("/[\x{4e00}-\x{9fa5}]+/u", $charset, $ipadds);
  202. return $ipadds; // 返回一个二维数组
  203. } else {
  204. return "addree is none";
  205. }
  206. }
  207. //获得本地真实IP
  208. // function get_onlineip()
  209. // {
  210. // $mip = file_get_contents("http://city.ip138.com/city0.asp");
  211. // if ($mip) {
  212. // preg_match("/\[.*\]/", $mip, $sip);
  213. // $p = array(
  214. // "/\[/",
  215. // "/\]/"
  216. // );
  217. // return preg_replace($p, "", $sip[0]);
  218. // } else {
  219. // return "获取本地IP失败!";
  220. // }
  221. // }
  222. }
  223. // $info = new get_equipment_info();
  224. // echo json_decode($info -> GetLang());
  225. // echo json_decode($info -> GetOs());
  226. // echo json_decode($info -> GetBrowser());
  227. // print_r($info -> Getaddress());
  228. // echo $info -> Getip();
  229. // echo $info -> get_onlineip();
  230. // die;
  231. ?>