Home > Backend Development > PHP Tutorial > PHP class to obtain client information

PHP class to obtain client information

WBOY
Release: 2016-07-25 08:56:07
Original
1036 people have browsed it
  1. /**
  2. * Classes for obtaining visitor information: language, browser, operating system, IP, geographical location, ISP.
  3. * Date: 2013/10/11
  4. * Editor: bbs.it-home.org
  5. * Use:
  6. $obj = new class_guest_info;
  7. $obj->GetLang(); //Get guest language: Simplified Chinese, Traditional Chinese, English.
  8. $obj->GetBrowser(); //Get the visitor's browser: MSIE, Firefox, Chrome, Safari, Opera, Other.
  9. $obj->GetOS(); //Get the guest operating system: Windows, MAC, Linux, Unix, BSD, Other.
  10. $obj->GetIP(); //Get the visitor IP address.
  11. $obj->GetAdd(); //Get the visitor's geographical location and use Baidu hidden interface.
  12. $obj->GetIsp(); //Get the guest ISP, use Baidu hidden interface.
  13. */
  14. class class_guest_info{
  15. function GetLang() {
  16. $Lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4);
  17. // Use substr() to intercept the string, starting from 0, intercepting 4 characters
  18. if (preg_match('/zh-c/i',$Lang)) {
  19. //preg_match() regular expression matching function
  20. $Lang = 'Simplified Chinese';
  21. }
  22. elseif (preg_match('/zh/i',$Lang)) {
  23. $Lang = 'Traditional Chinese';
  24. }
  25. else {
  26. $Lang = 'English';
  27. }
  28. return $Lang;
  29. }
  30. function GetBrowser() {
  31. $Browser = $_SERVER['HTTP_USER_AGENT'];
  32. if (preg_match('/MSIE/i',$Browser)) {
  33. $Browser = 'MSIE';
  34. }
  35. elseif (preg_match('/Firefox/i',$Browser)) {
  36. $Browser = 'Firefox';
  37. }
  38. elseif (preg_match('/Chrome/i',$Browser)) {
  39. $Browser = ' Chrome';
  40. }
  41. elseif (preg_match('/Safari/i',$Browser)) {
  42. $Browser = 'Safari';
  43. }
  44. elseif (preg_match('/Opera/i',$Browser)) {
  45. $Browser = 'Opera';
  46. }
  47. else {
  48. $Browser = 'Other';
  49. }
  50. return $Browser;
  51. }
  52. function GetOS() {
  53. $OS = $_SERVER['HTTP_USER_AGENT'];
  54. if ( preg_match('/win/i',$OS)) {
  55. $OS = 'Windows';
  56. }
  57. elseif (preg_match('/mac/i',$OS)) {
  58. $OS = 'MAC';
  59. }
  60. elseif (preg_match('/linux/i',$OS)) {
  61. $OS = 'Linux';
  62. }
  63. elseif (preg_match('/unix/i',$OS)) {
  64. $OS = ' Unix';
  65. }
  66. elseif (preg_match('/bsd/i',$OS)) {
  67. $OS = 'BSD';
  68. }
  69. else {
  70. $OS = 'Other';
  71. }
  72. return $OS;
  73. }
  74. function GetIP() {
  75. if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  76. //If the variable is a non-empty or non-zero value, empty() returns FALSE.
  77. $IP = explode(',',$_SERVER['HTTP_CLIENT_IP']);
  78. }
  79. elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  80. $IP = explode(',',$_SERVER[ 'HTTP_X_FORWARDED_FOR']);
  81. }
  82. elseif (!empty($_SERVER['REMOTE_ADDR'])) {
  83. $IP = explode(',',$_SERVER['REMOTE_ADDR']);
  84. }
  85. else {
  86. $ IP[0] = 'None';
  87. }
  88. return $IP[0];
  89. }
  90. private function GetAddIsp() {
  91. $IP = $this->GetIP();
  92. $AddIsp = mb_convert_encoding(file_get_contents(' http://open.baidu.com/ipsearch/s?tn=ipjson&wd='.$IP),'UTF-8','GBK');
  93. //mb_convert_encoding() Convert character encoding.
  94. if (preg_match('/noresult/i',$AddIsp)) {
  95. $AddIsp = 'None';
  96. }
  97. else {
  98. $Sta = stripos($AddIsp,$IP) + strlen($IP) + strlen ('from');
  99. $Len = stripos($AddIsp,'"}')-$Sta;
  100. $AddIsp = substr($AddIsp,$Sta,$Len);
  101. }
  102. $AddIsp = explode(' ' ,$AddIsp);
  103. return $AddIsp;
  104. }
  105. function GetAdd() {
  106. $Add = $this->GetAddIsp();
  107. return $Add[0];
  108. }
  109. function GetIsp() {
  110. $Isp = $this->GetAddIsp();
  111. if ($Isp[0] != 'None' && isset($Isp[1])) {
  112. $Isp = $Isp[1];
  113. }
  114. else {
  115. $Isp = 'None';
  116. }
  117. return $Isp;
  118. }
  119. }
  120. ?>
Copy code

>>>Articles you may be interested in: PHP Sina interface to query IP geographical location php Tencent IP sharing plan to obtain IP geographical location php gets geographical location by IP PHP gets the geolocation code via IP Code sharing for php to obtain website location and operating system information An example reference for php to obtain geographical location through IP php implementation code to obtain the user’s real IP and geographical location (Taobao IP interface) php gets current geographical location interface based on IP address



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template