Home >Backend Development >PHP Tutorial >PHP code to obtain the client IP address, geographical information, browser information, and local real IP

PHP code to obtain the client IP address, geographical information, browser information, and local real IP

WBOY
WBOYOriginal
2016-07-25 09:04:35940browse
  1. // Function to get the client’s IP, geographical information, and browser
  2. class get_gust_info {
  3. ////Get the visitor’s browser type
  4. function GetBrowser(){
  5. if(!empty($ _SERVER['HTTP_USER_AGENT'])){
  6. $br = $_SERVER['HTTP_USER_AGENT'];
  7. if (preg_match('/MSIE/i',$br)) {
  8. $br = 'MSIE';
  9. }elseif ( preg_match('/Firefox/i',$br)) {
  10. $br = 'Firefox';
  11. }elseif (preg_match('/Chrome/i',$br)) {
  12. $br = 'Chrome';
  13. } elseif (preg_match('/Safari/i',$br)) {
  14. $br = 'Safari';
  15. }elseif (preg_match('/Opera/i',$br)) {
  16. $br = 'Opera';
  17. }else {
  18. $br = 'Other';
  19. }
  20. return $br;
  21. }else{return "Failed to obtain browser information!";}
  22. }
  23. ////Get visitor browser language
  24. function GetLang (){
  25. if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
  26. $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  27. $lang = substr($lang,0,5);
  28. if(preg_match( "/zh-cn/i",$lang)){
  29. $lang = "Simplified Chinese";
  30. }elseif(preg_match("/zh/i",$lang)){
  31. $lang = "Traditional Chinese";
  32. }else{
  33. $lang = "English";
  34. }
  35. return $lang;
  36. }else{return "Failed to obtain browser language! ";}
  37. }
  38. ////Get the guest operating system
  39. function GetOs(){
  40. if(!empty($_SERVER['HTTP_USER_AGENT'])){
  41. $OS = $_SERVER['HTTP_USER_AGENT'];
  42. if (preg_match('/win/i',$OS)) {
  43. $OS = 'Windows';
  44. }elseif (preg_match('/mac/i',$OS)) {
  45. $OS = 'MAC';
  46. }elseif (preg_match('/linux/i',$OS)) {
  47. $OS = 'Linux';
  48. }elseif (preg_match('/unix/i',$OS)) {
  49. $OS = 'Unix ';
  50. }elseif (preg_match('/bsd/i',$OS)) {
  51. $OS = 'BSD';
  52. }else {
  53. $OS = 'Other';
  54. }
  55. return $OS;
  56. }else {return "Failed to obtain guest operating system information! ";}
  57. }
  58. ////Get the visitor's real ip
  59. function Getip(){
  60. if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  61. $ip = $_SERVER["HTTP_CLIENT_IP"];
  62. }
  63. if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //Get proxy ip
  64. $ips = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
  65. }
  66. if($ip){
  67. $ips = array_unshift($ips,$ip);
  68. }
  69. $count = count($ips);
  70. for($i=0;$i<$count;$i++){
  71. if(!preg_match( "/^(10|172.16|192.168)./i",$ips[$i])){//Exclude LAN ip
  72. $ip = $ips[$i];
  73. break;
  74. }
  75. }
  76. $tip = empty($_SERVER['REMOTE_ADDR']) ? $ip : $_SERVER['REMOTE_ADDR'];
  77. if($tip=="127.0.0.1"){ //Get the local real IP
  78. return $this-> get_onlineip();
  79. }else{
  80. return $tip;
  81. }
  82. }
  83. ////Get the local real IP
  84. function get_onlineip() {
  85. $mip = file_get_contents("http://city.ip138.com/ city0.asp");
  86. if($mip){
  87. preg_match("/[.*]/",$mip,$sip);
  88. $p = array("/[/","/]/") ;
  89. return preg_replace($p,"",$sip[0]);
  90. }else{return "Failed to obtain local IP! ";}
  91. }
  92. ////Get the visitor's location name based on ip
  93. function Getaddress($ip=''){
  94. if(empty($ip)){
  95. $ip = $this->Getip() ;
  96. }
  97. $ipadd = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=".$ip);//According to Sina api interface to get
  98. if($ipadd ){
  99. $charset = iconv("gbk","utf-8",$ipadd);
  100. preg_match_all("/[x{4e00}-x{9fa5}]+/u",$charset,$ipadds);
  101. return $ipadds; //Return a two-dimensional array
  102. }else{return "addree is none";}
  103. }
  104. }
  105. $gifo = new get_gust_info();
  106. echo "Your ip:".$gifo- >Getip();
  107. echo "
    Location:";
  108. $ipadds = $gifo->Getaddress();
  109. foreach($ipadds[0] as $value){
  110. echo "rn " .iconv("utf-8","gbk",$value);
  111. }
  112. echo "
    Browser type:".$gifo->GetBrowser();
  113. echo "
    Browser language: ".$gifo->GetLang();
  114. echo "
    Operating system: ".$gifo->GetOs();
  115. ?>
Copy code

>>>> Articles you may be interested in: Get the php code of the user’s real IP address Two ways to get the real IP of the external network with php php code to get accurate client IP address Introduction to the method of obtaining the real IP of the client in php 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



Statement:
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