Maison > développement back-end > tutoriel php > PHP获取客户端操作系统,浏览器,语言,IP,IP归属地等

PHP获取客户端操作系统,浏览器,语言,IP,IP归属地等

WBOY
Libérer: 2016-07-25 08:50:18
original
1370 Les gens l'ont consulté
PHP获取客户端操作系统,浏览器,语言,IP,IP归属地等
希望能楼下的继续补充啊 完善这个东东

2013.8.12 更新了 下 更完善了
  1. class Client
  2. {
  3. function CID_windows_detect_os($ua) {
  4. $os_name = $os_code = $os_ver = $pda_name = $pda_code = $pda_ver = null;
  5. if (preg_match('/Windows 95/i', $ua) || preg_match('/Win95/', $ua)) {
  6. $os_name = "Windows";
  7. $os_code = "windows";
  8. $os_ver = "95";
  9. } elseif (preg_match('/Windows NT 5.0/i', $ua) || preg_match('/Windows 2000/i', $ua)) {
  10. $os_name = "Windows";
  11. $os_code = "windows";
  12. $os_ver = "2000";
  13. } elseif (preg_match('/Win 9x 4.90/i', $ua) || preg_match('/Windows ME/i', $ua)) {
  14. $os_name = "Windows";
  15. $os_code = "windows";
  16. $os_ver = "ME";
  17. } elseif (preg_match('/Windows.98/i', $ua) || preg_match('/Win98/i', $ua)) {
  18. $os_name = "Windows";
  19. $os_code = "windows";
  20. $os_ver = "98";
  21. } elseif (preg_match('/Windows NT 6.0/i', $ua)) {
  22. $os_name = "Windows";
  23. $os_code = "windows_vista";
  24. $os_ver = "Vista";
  25. } elseif (preg_match('/Windows NT 6.1/i', $ua)) {
  26. $os_name = "Windows";
  27. $os_code = "windows_win7";
  28. $os_ver = "7";
  29. } elseif (preg_match('/Windows NT 6.2/i', $ua)) {
  30. $os_name = "Windows";
  31. $os_code = "windows_win8";
  32. $os_ver = "8";
  33. } elseif (preg_match('/Windows NT 5.1/i', $ua)) {
  34. $os_name = "Windows";
  35. $os_code = "windows";
  36. $os_ver = "XP";
  37. } elseif (preg_match('/Windows NT 5.2/i', $ua)) {
  38. $os_name = "Windows";
  39. $os_code = "windows";
  40. if (preg_match('/Win64/i', $ua)) {
  41. $os_ver = "XP 64 bit";
  42. } else {
  43. $os_ver = "Server 2003";
  44. }
  45. }
  46. elseif (preg_match('/Mac_PowerPC/i', $ua)) {
  47. $os_name = "Mac OS";
  48. $os_code = "macos";
  49. }elseif (preg_match('/Windows Phone/i', $ua)) {
  50. $matches = explode(';',$ua);
  51. $os_name = $matches[2];
  52. $os_code = "windows_phone7";
  53. } elseif (preg_match('/Windows NT 4.0/i', $ua) || preg_match('/WinNT4.0/i', $ua)) {
  54. $os_name = "Windows";
  55. $os_code = "windows";
  56. $os_ver = "NT 4.0";
  57. } elseif (preg_match('/Windows NT/i', $ua) || preg_match('/WinNT/i', $ua)) {
  58. $os_name = "Windows";
  59. $os_code = "windows";
  60. $os_ver = "NT";
  61. } elseif (preg_match('/Windows CE/i', $ua)) {
  62. list($os_name, $os_code, $os_ver, $pda_name, $pda_code, $pda_ver) = self::CID_pda_detect_os($ua);
  63. $os_name = "Windows";
  64. $os_code = "windows";
  65. $os_ver = "CE";
  66. if (preg_match('/PPC/i', $ua)) {
  67. $os_name = "Microsoft PocketPC";
  68. $os_code = "windows";
  69. $os_ver = '';
  70. }
  71. if (preg_match('/smartphone/i', $ua)) {
  72. $os_name = "Microsoft Smartphone";
  73. $os_code = "windows";
  74. $os_ver = '';
  75. }
  76. } else{
  77. $os_name = 'Unknow Os';
  78. $os_code = 'other';
  79. }
  80. return array($os_name, $os_code, $os_ver, $pda_name, $pda_code, $pda_ver);
  81. }
  82. function CID_unix_detect_os($ua) {
  83. $os_name = $os_ver = $os_code = null;
  84. if (preg_match('/Linux/i', $ua)) {
  85. $os_name = "Linux";
  86. $os_code = "linux";
  87. if (preg_match('#Debian#i', $ua)) {
  88. $os_code = "debian";
  89. $os_name = "Debian GNU/Linux";
  90. } elseif (preg_match('#Mandrake#i', $ua)) {
  91. $os_code = "mandrake";
  92. $os_name = "Mandrake Linux";
  93. } elseif (preg_match('#Kindle Fire#i',$ua)) {//for Kindle Fire
  94. $matches = explode(';',$ua);
  95. $os_code = "kindle";
  96. $matches2 = explode(')',$matches[4]);
  97. $os_name = $matches[2].$matches2[0];
  98. } elseif (preg_match('#Android#i',$ua)) {//Android
  99. $matches = explode(';',$ua);
  100. $os_code = "android";
  101. $matches2 = explode(')',$matches[4]);
  102. $os_name = $matches[2].$matches2[0];
  103. } elseif (preg_match('#SuSE#i', $ua)) {
  104. $os_code = "suse";
  105. $os_name = "SuSE Linux";
  106. } elseif (preg_match('#Novell#i', $ua)) {
  107. $os_code = "novell";
  108. $os_name = "Novell Linux";
  109. } elseif (preg_match('#Ubuntu#i', $ua)) {
  110. $os_code = "ubuntu";
  111. $os_name = "Ubuntu Linux";
  112. } elseif (preg_match('#Red ?Hat#i', $ua)) {
  113. $os_code = "redhat";
  114. $os_name = "RedHat Linux";
  115. } elseif (preg_match('#Gentoo#i', $ua)) {
  116. $os_code = "gentoo";
  117. $os_name = "Gentoo Linux";
  118. } elseif (preg_match('#Fedora#i', $ua)) {
  119. $os_code = "fedora";
  120. $os_name = "Fedora Linux";
  121. } elseif (preg_match('#MEPIS#i', $ua)) {
  122. $os_name = "MEPIS Linux";
  123. } elseif (preg_match('#Knoppix#i', $ua)) {
  124. $os_name = "Knoppix Linux";
  125. } elseif (preg_match('#Slackware#i', $ua)) {
  126. $os_code = "slackware";
  127. $os_name = "Slackware Linux";
  128. } elseif (preg_match('#Xandros#i', $ua)) {
  129. $os_name = "Xandros Linux";
  130. } elseif (preg_match('#Kanotix#i', $ua)) {
  131. $os_name = "Kanotix Linux";
  132. }
  133. } elseif (preg_match('/FreeBSD/i', $ua)) {
  134. $os_name = "FreeBSD";
  135. $os_code = "freebsd";
  136. } elseif (preg_match('/NetBSD/i', $ua)) {
  137. $os_name = "NetBSD";
  138. $os_code = "netbsd";
  139. } elseif (preg_match('/OpenBSD/i', $ua)) {
  140. $os_name = "OpenBSD";
  141. $os_code = "openbsd";
  142. } elseif (preg_match('/IRIX/i', $ua)) {
  143. $os_name = "SGI IRIX";
  144. $os_code = "sgi";
  145. } elseif (preg_match('/SunOS/i', $ua)) {
  146. $os_name = "Solaris";
  147. $os_code = "sun";
  148. } elseif (preg_match('#iPod.*.CPU.([a-zA-Z0-9.( _)]+)#i', $ua, $matches)) {
  149. $os_name = "iPod";
  150. $os_code = "iphone";
  151. $os_ver = $matches[1];
  152. } elseif (preg_match('#iPhone.*.CPU.([a-zA-Z0-9.( _)]+)#i', $ua, $matches)) {
  153. $os_name = "iPhone";
  154. $os_code = "iphone";
  155. $os_ver = $matches[1];
  156. } elseif (preg_match('#iPad.*.CPU.([a-zA-Z0-9.( _)]+)#i', $ua, $matches)) {
  157. $os_name = "iPad";
  158. $os_code = "ipad";
  159. $os_ver = $matches[1];
  160. } elseif (preg_match('/Mac OS X.([0-9. _]+)/i', $ua, $matches)) {
  161. $os_name = "Mac OS";
  162. $os_code = "macos";
  163. if(count(explode(7,$matches[1]))>1) $matches[1] = 'Lion '.$matches[1];
  164. elseif(count(explode(8,$matches[1]))>1) $matches[1] = 'Mountain Lion '.$matches[1];
  165. $os_ver = "X ".$matches[1];
  166. } elseif (preg_match('/Macintosh/i', $ua)) {
  167. $os_name = "Mac OS";
  168. $os_code = "macos";
  169. } elseif (preg_match('/Unix/i', $ua)) {
  170. $os_name = "UNIX";
  171. $os_code = "unix";
  172. } elseif (preg_match('/CrOS/i', $ua)){
  173. $os_name="Google Chrome OS";
  174. $os_code="chromeos";
  175. } elseif (preg_match('/Fedor.([0-9. _]+)/i', $ua, $matches)){
  176. $os_name="Fedora";
  177. $os_code="fedora";
  178. $os_ver = $matches[1];
  179. } else{
  180. $os_name = 'Unknow Os';
  181. $os_code = 'other';
  182. }
  183. return array($os_name, $os_code, $os_ver);
  184. }
  185. function CID_pda_detect_os($ua) {
  186. $os_name = $os_code = $os_ver = $pda_name = $pda_code = $pda_ver = null;
  187. if (preg_match('#PalmOS#i', $ua)) {
  188. $os_name = "Palm OS";
  189. $os_code = "palm";
  190. } elseif (preg_match('#Windows CE#i', $ua)) {
  191. $os_name = "Windows CE";
  192. $os_code = "windows";
  193. } elseif (preg_match('#QtEmbedded#i', $ua)) {
  194. $os_name = "Qtopia";
  195. $os_code = "linux";
  196. } elseif (preg_match('#Zaurus#i', $ua)) {
  197. $os_name = "Linux";
  198. $os_code = "linux";
  199. } elseif (preg_match('#Symbian#i', $ua)) {
  200. $os_name = "Symbian OS";
  201. $os_code = "symbian";
  202. } elseif (preg_match('#PalmOS/sony/model#i', $ua)) {
  203. $pda_name = "Sony Clie";
  204. $pda_code = "sony";
  205. } elseif (preg_match('#Zaurus ([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  206. $pda_name = "Sharp Zaurus " . $matches[1];
  207. $pda_code = "zaurus";
  208. $pda_ver = $matches[1];
  209. } elseif (preg_match('#Series ([0-9]+)#i', $ua, $matches)) {
  210. $pda_name = "Series";
  211. $pda_code = "nokia";
  212. $pda_ver = $matches[1];
  213. } elseif (preg_match('#Nokia ([0-9]+)#i', $ua, $matches)) {
  214. $pda_name = "Nokia";
  215. $pda_code = "nokia";
  216. $pda_ver = $matches[1];
  217. } elseif (preg_match('#SIE-([a-zA-Z0-9]+)#i', $ua, $matches)) {
  218. $pda_name = "Siemens";
  219. $pda_code = "siemens";
  220. $pda_ver = $matches[1];
  221. } elseif (preg_match('#dopod([a-zA-Z0-9]+)#i', $ua, $matches)) {
  222. $pda_name = "Dopod";
  223. $pda_code = "dopod";
  224. $pda_ver = $matches[1];
  225. } elseif (preg_match('#o2 xda ([a-zA-Z0-9 ]+);#i', $ua, $matches)) {
  226. $pda_name = "O2 XDA";
  227. $pda_code = "o2";
  228. $pda_ver = $matches[1];
  229. } elseif (preg_match('#SEC-([a-zA-Z0-9]+)#i', $ua, $matches)) {
  230. $pda_name = "Samsung";
  231. $pda_code = "samsung";
  232. $pda_ver = $matches[1];
  233. } elseif (preg_match('#SonyEricsson ?([a-zA-Z0-9]+)#i', $ua, $matches)) {
  234. $pda_name = "SonyEricsson";
  235. $pda_code = "sonyericsson";
  236. $pda_ver = $matches[1];
  237. } elseif (preg_match('#Kindle\/([a-zA-Z0-9. ×\(.\)]+)#i',$ua, $matches)) {//for Kindle
  238. $pda_name = "kindle";
  239. $pda_code = "kindle";
  240. $pda_ver = $matches[1];
  241. } else {
  242. $pda_name = 'Unknow Os';
  243. $pda_code = 'other';
  244. }
  245. return array($os_name, $os_code, $os_ver, $pda_name, $pda_code, $pda_ver);
  246. }
  247. function CID_detect_browser($ua) {
  248. $browser_name = $browser_code = $browser_ver = $os_name = $os_code = $os_ver = $pda_name = $pda_code = $pda_ver = null;
  249. $ua = preg_replace("/FunWebProducts/i", "", $ua);
  250. if (preg_match('#MovableType[ /]([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  251. $browser_name = 'MovableType';
  252. $browser_code = 'mt';
  253. $browser_ver = $matches[1];
  254. } elseif (preg_match('#WordPress[ /]([a-zA-Z0-9.]*)#i', $ua, $matches)) {
  255. $browser_name = 'WordPress';
  256. $browser_code = 'wp';
  257. $browser_ver = $matches[1];
  258. } elseif (preg_match('#typepad[ /]([a-zA-Z0-9.]*)#i', $ua, $matches)) {
  259. $browser_name = 'TypePad';
  260. $browser_code = 'typepad';
  261. $browser_ver = $matches[1];
  262. } elseif (preg_match('#drupal#i', $ua)) {
  263. $browser_name = 'Drupal';
  264. $browser_code = 'drupal';
  265. $browser_ver = count($matches) > 0 ? $matches[1] : "";
  266. } elseif (preg_match('#symbianos/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  267. $os_name = "SymbianOS";
  268. $os_ver = $matches[1];
  269. $os_code = 'symbian';
  270. } elseif (preg_match('#avantbrowser.com#i', $ua)) {
  271. $browser_name = 'Avant Browser';
  272. $browser_code = 'avantbrowser';
  273. } elseif (preg_match('#(Camino|Chimera)[ /]([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  274. $browser_name = 'Camino';
  275. $browser_code = 'camino';
  276. $browser_ver = $matches[2];
  277. $os_name = "Mac OS";
  278. $os_code = "macos";
  279. $os_ver = "X";
  280. } elseif (preg_match('#anonymouse#i', $ua, $matches)) {
  281. $browser_name = 'Anonymouse';
  282. $browser_code = 'anonymouse';
  283. } elseif (preg_match('#PHP#', $ua, $matches)) {
  284. $browser_name = 'PHP';
  285. $browser_code = 'php';
  286. } elseif (preg_match('#danger hiptop#i', $ua, $matches)) {
  287. $browser_name = 'Danger HipTop';
  288. $browser_code = 'danger';
  289. } elseif (preg_match('#w3m/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  290. $browser_name = 'W3M';
  291. $browser_code = 'w3m';
  292. $browser_ver = $matches[1];
  293. } elseif (preg_match('#Shiira[/]([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  294. $browser_name = 'Shiira';
  295. $browser_code = 'shiira';
  296. $browser_ver = $matches[1];
  297. $os_name = "Mac OS";
  298. $os_code = "macos";
  299. $os_ver = "X";
  300. } elseif (preg_match('#Dillo[ /]([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  301. $browser_name = 'Dillo';
  302. $browser_code = 'dillo';
  303. $browser_ver = $matches[1];
  304. } elseif (preg_match('#Epiphany/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  305. $browser_name = 'Epiphany';
  306. $browser_code = 'epiphany';
  307. $browser_ver = $matches[1];
  308. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  309. } elseif (preg_match('#UP.Browser/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  310. $browser_name = 'Openwave UP.Browser';
  311. $browser_code = 'openwave';
  312. $browser_ver = $matches[1];
  313. } elseif (preg_match('#DoCoMo/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  314. $browser_name = 'DoCoMo';
  315. $browser_code = 'docomo';
  316. $browser_ver = $matches[1];
  317. if ($browser_ver == '1.0') {
  318. preg_match('#DoCoMo/([a-zA-Z0-9.]+)/([a-zA-Z0-9.]+)#i', $ua, $matches);
  319. $browser_ver = $matches[2];
  320. } elseif ($browser_ver == '2.0') {
  321. preg_match('#DoCoMo/([a-zA-Z0-9.]+) ([a-zA-Z0-9.]+)#i', $ua, $matches);
  322. $browser_ver = $matches[2];
  323. }
  324. } elseif (preg_match('#(SeaMonkey)/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  325. $browser_name = 'Mozilla SeaMonkey';
  326. $browser_code = 'seamonkey';
  327. $browser_ver = $matches[2];
  328. if (preg_match('/Windows/i', $ua)) {
  329. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  330. } else {
  331. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  332. }
  333. } elseif (preg_match('#Kazehakase/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  334. $browser_name = 'Kazehakase';
  335. $browser_code = 'kazehakase';
  336. $browser_ver = $matches[1];
  337. if (preg_match('/Windows/i', $ua)) {
  338. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  339. } else {
  340. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  341. }
  342. } elseif (preg_match('#Flock/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  343. $browser_name = 'Flock';
  344. $browser_code = 'flock';
  345. $browser_ver = $matches[1];
  346. if (preg_match('/Windows/i', $ua)) {
  347. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  348. } else {
  349. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  350. }
  351. } elseif (preg_match('#(Firefox|Phoenix|Firebird|BonEcho|GranParadiso|Minefield|Iceweasel)/4([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  352. $browser_name = 'Mozilla Firefox';
  353. $browser_code = 'firefox';
  354. $browser_ver = '4'.$matches[2];
  355. if (preg_match('/Windows/i', $ua)) {
  356. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  357. } else {
  358. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  359. }
  360. } elseif (preg_match('#(Firefox|Phoenix|Firebird|BonEcho|GranParadiso|Minefield|Iceweasel)/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  361. $browser_name = 'Mozilla Firefox';
  362. $browser_code = 'firefox';
  363. $browser_ver = $matches[2];
  364. if (preg_match('/Windows/i', $ua)) {
  365. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  366. } else {
  367. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  368. }
  369. } elseif (preg_match('#Minimo/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  370. $browser_name = 'Minimo';
  371. $browser_code = 'mozilla';
  372. $browser_ver = $matches[1];
  373. if (preg_match('/Windows/i', $ua)) {
  374. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  375. } else {
  376. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  377. }
  378. } elseif (preg_match('#MultiZilla/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  379. $browser_name = 'MultiZilla';
  380. $browser_code = 'mozilla';
  381. $browser_ver = $matches[1];
  382. if (preg_match('/Windows/i', $ua)) {
  383. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  384. } else {
  385. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  386. }
  387. } elseif (preg_match('#SE 2([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  388. $browser_name = 'SouGou Browser';
  389. $browser_code = 'sogou';
  390. $browser_ver = '2'.$matches[1];
  391. if (preg_match('/Windows/i', $ua)) {
  392. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  393. } else {
  394. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  395. }
  396. } elseif (preg_match('#baidubrowser ([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  397. $browser_name = 'BaiDu Browser';
  398. $browser_code = 'baidubrowser';
  399. $browser_ver = $matches[1];
  400. if (preg_match('/Windows/i', $ua)) {
  401. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  402. } else {
  403. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  404. }
  405. } elseif (preg_match('#360([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  406. $browser_name = '360 Browser';
  407. $browser_code = '360se';
  408. $browser_ver = $matches[1];
  409. if (preg_match('/Windows/i', $ua)) {
  410. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  411. } else {
  412. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  413. }
  414. } elseif (preg_match('#QQBrowser/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  415. $browser_name = 'QQ Browser';
  416. $browser_code = 'qqbrowser';
  417. $browser_ver = $matches[1];
  418. if (preg_match('/Windows/i', $ua)) {
  419. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  420. } else {
  421. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  422. }
  423. } elseif (preg_match('/PSP \(PlayStation Portable\)\; ([a-zA-Z0-9.]+)/', $ua, $matches)) {
  424. $pda_name = "Sony PSP";
  425. $pda_code = "sony-psp";
  426. $pda_ver = $matches[1];
  427. } elseif (preg_match('#Galeon/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  428. $browser_name = 'Galeon';
  429. $browser_code = 'galeon';
  430. $browser_ver = $matches[1];
  431. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  432. } elseif (preg_match('#iCab/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  433. $browser_name = 'iCab';
  434. $browser_code = 'icab';
  435. $browser_ver = $matches[1];
  436. $os_name = "Mac OS";
  437. $os_code = "macos";
  438. if (preg_match('#Mac OS X#i', $ua)) {
  439. $os_ver = "X";
  440. }
  441. } elseif (preg_match('#K-Meleon/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  442. $browser_name = 'K-Meleon';
  443. $browser_code = 'kmeleon';
  444. $browser_ver = $matches[1];
  445. if (preg_match('/Windows/i', $ua)) {
  446. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  447. } else {
  448. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  449. }
  450. } elseif (preg_match('#Lynx/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  451. $browser_name = 'Lynx';
  452. $browser_code = 'lynx';
  453. $browser_ver = $matches[1];
  454. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  455. } elseif (preg_match('#Links \\(([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  456. $browser_name = 'Links';
  457. $browser_code = 'lynx';
  458. $browser_ver = $matches[1];
  459. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  460. } elseif (preg_match('#ELinks[/ ]([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  461. $browser_name = 'ELinks';
  462. $browser_code = 'lynx';
  463. $browser_ver = $matches[1];
  464. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  465. } elseif (preg_match('#ELinks \\(([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  466. $browser_name = 'ELinks';
  467. $browser_code = 'lynx';
  468. $browser_ver = $matches[1];
  469. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  470. } elseif (preg_match('#Konqueror/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  471. $browser_name = 'Konqueror';
  472. $browser_code = 'konqueror';
  473. $browser_ver = $matches[1];
  474. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  475. if (!$os_name) {
  476. list($os_name, $os_code, $os_ver) = self::CID_pda_detect_os($ua);
  477. }
  478. } elseif (preg_match('#NetPositive/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  479. $browser_name = 'NetPositive';
  480. $browser_code = 'netpositive';
  481. $browser_ver = $matches[1];
  482. $os_name = "BeOS";
  483. $os_code = "beos";
  484. } elseif (preg_match('#OmniWeb#i', $ua)) {
  485. $browser_name = 'OmniWeb';
  486. $browser_code = 'omniweb';
  487. $os_name = "Mac OS";
  488. $os_code = "macos";
  489. $os_ver = "X";
  490. } elseif (preg_match('#Chrome/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  491. $browser_name = 'Google Chrome'; $browser_code = 'chrome'; $browser_ver = $matches[1];
  492. if (preg_match('/Windows/i', $ua)) {
  493. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  494. } else {
  495. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  496. }
  497. } elseif (preg_match('#Arora/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  498. $browser_name = 'Arora';
  499. $browser_code = 'arora';
  500. $browser_ver = $matches[1];
  501. if (preg_match('/Windows/i', $ua)) {
  502. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  503. } else {
  504. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  505. }
  506. } elseif (preg_match('#Maxthon( |\/)([a-zA-Z0-9.]+)#i', $ua,$matches)) {
  507. $browser_name = 'Maxthon';
  508. $browser_code = 'maxthon';
  509. $browser_ver = $matches[2];
  510. if (preg_match('/Win/i', $ua)) {
  511. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  512. } else {
  513. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  514. }
  515. } elseif (preg_match('#CriOS/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  516. $browser_name = 'Chrome for iOS';
  517. $browser_code = 'crios';
  518. $browser_ver = $matches[1];
  519. if (preg_match('/Windows/i', $ua)) {
  520. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  521. } else {
  522. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  523. }
  524. } elseif (preg_match('#Safari/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  525. $browser_name = 'Safari';
  526. $browser_code = 'safari';
  527. $browser_ver = $matches[1];
  528. if (preg_match('/Windows/i', $ua)) {
  529. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  530. } else {
  531. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  532. }
  533. } elseif (preg_match('#opera mini#i', $ua)) {
  534. $browser_name = 'Opera Mini';
  535. $browser_code = 'opera';
  536. preg_match('#Opera/([a-zA-Z0-9.]+)#i', $ua, $matches);
  537. $browser_ver = $matches[1];
  538. if (preg_match('/Windows/i', $ua)) {
  539. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  540. } else {
  541. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  542. }
  543. } elseif (preg_match('#Opera.(.*)Version[ /]([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  544. $browser_name = 'Opera';
  545. $browser_code = 'opera';
  546. $browser_ver = $matches[2];
  547. if (preg_match('/Windows/i', $ua)) {
  548. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  549. } else {
  550. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  551. }
  552. if (!$os_name) {
  553. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  554. }
  555. if (!$os_name) {
  556. list($os_name, $os_code, $os_ver, $pda_name, $pda_code, $pda_ver) = self::CID_pda_detect_os($ua);
  557. }
  558. if (!$os_name) {
  559. if (preg_match('/Wii/i', $ua)) {
  560. $os_name = "Nintendo Wii";
  561. $os_code = "nintendo-wii";
  562. }
  563. }
  564. } elseif (preg_match('#Opera/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  565. $browser_name = 'Opera Mini';
  566. $browser_code = 'opera';
  567. $browser_ver = $matches[1];
  568. if (preg_match('/Windows/i', $ua)) {
  569. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  570. } else {
  571. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  572. }
  573. } elseif (preg_match('#WebPro/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  574. $browser_name = 'WebPro';
  575. $browser_code = 'webpro';
  576. $browser_ver = $matches[1];
  577. $os_name = "PalmOS";
  578. $os_code = "palmos";
  579. } elseif (preg_match('#WebPro#i', $ua, $matches)) {
  580. $browser_name = 'WebPro';
  581. $browser_code = 'webpro';
  582. $os_name = "PalmOS";
  583. $os_code = "palmos";
  584. } elseif (preg_match('#Netfront/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  585. $browser_name = 'Netfront';
  586. $browser_code = 'netfront';
  587. $browser_ver = $matches[1];
  588. list($os_name, $os_code, $os_ver, $pda_name, $pda_code, $pda_ver) = self::CID_pda_detect_os($ua);
  589. } elseif (preg_match('#Xiino/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  590. $browser_name = 'Xiino';
  591. $browser_code = 'xiino';
  592. $browser_ver = $matches[1];
  593. } elseif (preg_match('/wp-blackberry\/([a-zA-Z0-9.]*)/i', $ua, $matches)) {
  594. $browser_name = "WordPress for BlackBerry";
  595. $browser_code = "wordpress";
  596. $browser_ver = $matches[1];
  597. $pda_name = "BlackBerry";
  598. $pda_code = "blackberry";
  599. } elseif (preg_match('#Blackberry([0-9]+)#i', $ua, $matches)) {
  600. $pda_name = "Blackberry";
  601. $pda_code = "blackberry";
  602. $pda_ver = $matches[1];
  603. } elseif (preg_match('#Blackberry#i', $ua)) {
  604. $pda_name = "Blackberry";
  605. $pda_code = "blackberry";
  606. } elseif (preg_match('#SPV ([0-9a-zA-Z.]+)#i', $ua, $matches)) {
  607. $pda_name = "Orange SPV";
  608. $pda_code = "orange";
  609. $pda_ver = $matches[1];
  610. } elseif (preg_match('#LGE-([a-zA-Z0-9]+)#i', $ua, $matches)) {
  611. $pda_name = "LG";
  612. $pda_code = 'lg';
  613. $pda_ver = $matches[1];
  614. } elseif (preg_match('#MOT-([a-zA-Z0-9]+)#i', $ua, $matches)) {
  615. $pda_name = "Motorola";
  616. $pda_code = 'motorola';
  617. $pda_ver = $matches[1];
  618. } elseif (preg_match('#Nokia ?([0-9]+)#i', $ua, $matches)) {
  619. $pda_name = "Nokia";
  620. $pda_code = "nokia";
  621. $pda_ver = $matches[1];
  622. } elseif (preg_match('#NokiaN-Gage#i', $ua)) {
  623. $pda_name = "Nokia";
  624. $pda_code = "nokia";
  625. $pda_ver = "N-Gage";
  626. } elseif (preg_match('#Blazer[ /]?([a-zA-Z0-9.]*)#i', $ua, $matches)) {
  627. $browser_name = "Blazer";
  628. $browser_code = "blazer";
  629. $browser_ver = $matches[1];
  630. $os_name = "Palm OS";
  631. $os_code = "palm";
  632. } elseif (preg_match('#SIE-([a-zA-Z0-9]+)#i', $ua, $matches)) {
  633. $pda_name = "Siemens";
  634. $pda_code = "siemens";
  635. $pda_ver = $matches[1];
  636. } elseif (preg_match('#SEC-([a-zA-Z0-9]+)#i', $ua, $matches)) {
  637. $pda_name = "Samsung";
  638. $pda_code = "samsung";
  639. $pda_ver = $matches[1];
  640. } elseif (preg_match('/wp-iphone\/([a-zA-Z0-9.]*)/i', $ua, $matches)) {
  641. $browser_name = "WordPress for iOS";
  642. $browser_code = "wordpress";
  643. $browser_ver = $matches[1];
  644. $pda_name = "iPhone & iPad";
  645. $pda_code = "ipad";
  646. } elseif (preg_match('/wp-android\/([a-zA-Z0-9.]*)/i', $ua, $matches)) {
  647. $browser_name = "WordPress for Android";
  648. $browser_code = "wordpress";
  649. $browser_ver = $matches[1];
  650. $pda_name = "Android";
  651. $pda_code = "android";
  652. } elseif (preg_match('/wp-windowsphone\/([a-zA-Z0-9.]*)/i', $ua, $matches)) {
  653. $browser_name = "WordPress for Windows Phone 7";
  654. $browser_code = "wordpress";
  655. $browser_ver = $matches[1];
  656. $pda_name = "Windows Phone 7";
  657. $pda_code = "windows_phone7";
  658. } elseif (preg_match('/wp-nokia\/([a-zA-Z0-9.]*)/i', $ua, $matches)) {
  659. $browser_name = "WordPress for Nokia";
  660. $browser_code = "wordpress";
  661. $browser_ver = $matches[1];
  662. $pda_name = "Nokia";
  663. $pda_code = "nokia";
  664. } elseif (preg_match('#SAMSUNG-(S.H-[a-zA-Z0-9_/.]+)#i', $ua, $matches)) {
  665. $pda_name = "Samsung";
  666. $pda_code = "samsung";
  667. $pda_ver = $matches[1];
  668. if (preg_match('#(j2me|midp)#i', $ua)) {
  669. $browser_name = "J2ME/MIDP Browser";
  670. $browser_code = "j2me";
  671. }
  672. } elseif (preg_match('#SonyEricsson ?([a-zA-Z0-9]+)#i', $ua, $matches)) {
  673. $pda_name = "SonyEricsson";
  674. $pda_code = "sonyericsson";
  675. $pda_ver = $matches[1];
  676. } elseif (preg_match('#(j2me|midp)#i', $ua)) {
  677. $browser_name = "J2ME/MIDP Browser";
  678. $browser_code = "j2me";
  679. // mice
  680. } elseif (preg_match('/GreenBrowser/i', $ua)) {
  681. $browser_name = 'GreenBrowser';
  682. $browser_code = 'greenbrowser';
  683. if (preg_match('/Win/i', $ua)) {
  684. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  685. } else {
  686. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  687. }
  688. } elseif (preg_match('#TencentTraveler ([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  689. $browser_name = '腾讯TT浏览器';
  690. $browser_code = 'tencenttraveler';
  691. $browser_ver = $matches[1];
  692. if (preg_match('/Windows/i', $ua)) {
  693. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  694. } else {
  695. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  696. }
  697. } elseif (preg_match('#UCWEB([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  698. $browser_name = 'UCWEB';
  699. $browser_code = 'ucweb';
  700. $browser_ver = $matches[1];
  701. if (preg_match('/Windows/i', $ua)) {
  702. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  703. } else {
  704. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  705. }
  706. } elseif (preg_match('#MSIE ([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  707. $browser_name = 'Internet Explorer';
  708. $browser_ver = $matches[1];
  709. if ( strpos($browser_ver, '7') !== false || strpos($browser_ver, '8') !== false)
  710. $browser_code = 'ie8';
  711. elseif ( strpos($browser_ver, '9') !== false)
  712. $browser_code = 'ie9';
  713. elseif ( strpos($browser_ver, '10') !== false)
  714. $browser_code = 'ie10';
  715. else
  716. $browser_code = 'ie';
  717. list($os_name, $os_code, $os_ver, $pda_name, $pda_code, $pda_ver) = self::CID_windows_detect_os($ua);
  718. } elseif (preg_match('#Universe/([0-9.]+)#i', $ua, $matches)) {
  719. $browser_name = 'Universe';
  720. $browser_code = 'universe';
  721. $browser_ver = $matches[1];
  722. list($os_name, $os_code, $os_ver, $pda_name, $pda_code, $pda_ver) = self::CID_pda_detect_os($ua);
  723. }elseif (preg_match('#Netscape[0-9]?/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  724. $browser_name = 'Netscape';
  725. $browser_code = 'netscape';
  726. $browser_ver = $matches[1];
  727. if (preg_match('/Windows/i', $ua)) {
  728. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  729. } else {
  730. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  731. }
  732. } elseif (preg_match('#^Mozilla/5.0#i', $ua) && preg_match('#rv:([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  733. $browser_name = 'Mozilla';
  734. $browser_code = 'mozilla';
  735. $browser_ver = $matches[1];
  736. if (preg_match('/Windows/i', $ua)) {
  737. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  738. } else {
  739. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  740. }
  741. } elseif (preg_match('#^Mozilla/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
  742. $browser_name = 'Netscape Navigator';
  743. $browser_code = 'netscape';
  744. $browser_ver = $matches[1];
  745. if (preg_match('/Win/i', $ua)) {
  746. list($os_name, $os_code, $os_ver) = self::CID_windows_detect_os($ua);
  747. } else {
  748. list($os_name, $os_code, $os_ver) = self::CID_unix_detect_os($ua);
  749. }
  750. }else{
  751. $browser_name = 'Unknow Browser';
  752. $browser_code = 'null';
  753. }
  754. if (!$pda_name && !$os_name){
  755. $pda_name = 'Unknow Os';
  756. $pda_code = 'other';
  757. $os_name = 'Unknow Os';
  758. $os_code = 'other';
  759. }
  760. return array($browser_name, $browser_code, $browser_ver, $os_name, $os_code, $os_ver, $pda_name, $pda_code, $pda_ver);
  761. }
  762. ////获得访客浏览器类型
  763. function Get_Useragent(){
  764. if(!empty($_SERVER['HTTP_USER_AGENT'])){
  765. return self::CID_detect_browser($_SERVER['HTTP_USER_AGENT']);
  766. }
  767. else{
  768. return array();
  769. }
  770. }
  771. ////获得访客真实ip
  772. function Get_Ip_Addr(){
  773. if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  774. $ip = $_SERVER["HTTP_CLIENT_IP"];
  775. }
  776. if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //获取代理ip
  777. $ips = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
  778. }
  779. if($ip){
  780. $ips = array_unshift($ips,$ip);
  781. }
  782. $count = count($ips);
  783. for($i=0;$i if(!preg_match("/^(10|172\.16|192\.168)\./i",$ips[$i])){//排除局域网ip
  784. $ip = $ips[$i];
  785. break;
  786. }
  787. }
  788. $tip = empty($_SERVER['REMOTE_ADDR']) ? $ip : $_SERVER['REMOTE_ADDR'];
  789. if($tip=="127.0.0.1"){ //获得本地真实IP
  790. return self::get_onlineip();
  791. }
  792. else{
  793. return $tip;
  794. }
  795. }
  796. ////获得本地真实IP
  797. function get_onlineip() {
  798. $ip_json = @file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=myip");
  799. $ip_arr=json_decode(stripslashes($ip_json),1);
  800. if($ip_arr['code']==0)
  801. {
  802. return $ip_arr['data']['ip'];
  803. }
  804. }
  805. ////根据ip获得访客所在地地名
  806. function Get_Ip_From($ip=''){
  807. if(empty($ip)){
  808. $ip = self::Getip();
  809. }
  810. $ip_json=@file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".$ip);//根据taobao ip
  811. $ip_arr=json_decode(stripslashes($ip_json),1);
  812. if($ip_arr['code']==0)
  813. {
  814. return $ip_arr['data'];
  815. }
  816. else
  817. {
  818. return false;
  819. }
  820. }
  821. }
复制代码


source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal