Sharing tips on obtaining visitor IP, regional location, etc. with PHP

小云云
Release: 2023-03-20 08:46:01
Original
2166 people have browsed it

This article mainly introduces you to the relevant information on using PHP to obtain visitor IP, regional location, browser and source page information. The article provides detailed sample code for your reference and study, which has certain reference value for everyone. , friends who need it can come and take a look below.

Sample code

<?php
//这个类似用来获取访客信息的
//方便统计
class visitorInfo
{
 //获取访客ip
 public function getIp()
 {
  $ip=false;
  if(!empty($_SERVER["HTTP_CLIENT_IP"])){
   $ip = $_SERVER["HTTP_CLIENT_IP"];
  }
  if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
   $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
   if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
   for ($i = 0; $i < count($ips); $i++) {
    if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
     $ip = $ips[$i];
     break;
    }
   }
  }
  return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
 }

 //根据ip获取城市、网络运营商等信息
 public function findCityByIp($ip){
  $data = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip);
  return json_decode($data,$assoc=true);
 }

 //获取用户浏览器类型
 public function getBrowser(){
  $agent=$_SERVER["HTTP_USER_AGENT"];
  if(strpos($agent,'MSIE')!==false || strpos($agent,'rv:11.0')) //ie11判断
   return "ie";
  else if(strpos($agent,'Firefox')!==false)
   return "firefox";
  else if(strpos($agent,'Chrome')!==false)
   return "chrome";
  else if(strpos($agent,'Opera')!==false)
   return 'opera';
  else if((strpos($agent,'Chrome')==false)&&strpos($agent,'Safari')!==false)
   return 'safari';
  else
   return 'unknown';
 }

 //获取网站来源
 public function getFromPage(){
  return $_SERVER['HTTP_REFERER'];
 }

}
Copy after login

Related recommendations:

Detailed explanation of examples such as PHP getting visitor IP

php getting visitor IP Address method

PHP class for obtaining information such as visitor IP and geographical location

The above is the detailed content of Sharing tips on obtaining visitor IP, regional location, etc. with PHP. For more information, please follow other related articles on the PHP Chinese website!

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