Home >Backend Development >PHP Tutorial >Example analysis of how to obtain visitor information using PHP
Instance analysis of php implementation of obtaining visitor information
<?php header('Content-Type: text/html; charset=utf-8'); //获取访客的信息 class visitorInfo { //获取访客ip public function getIp() { $ip=false; if(!emptyempty($_SERVER["HTTP_CLIENT_IP"])){ $ip = $_SERVER["HTTP_CLIENT_IP"]; } if (!emptyempty($_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')) 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(){ if(isset($_SERVER['HTTP_REFERER'])) return $_SERVER['HTTP_REFERER']; else if(strpos($_SERVER['QUERY_STRING'], '?')) return 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; else return 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; } } $v = new visitorInfo(); echo $v -> getIp(),PHP_EOL; var_dump($v->findCityByIp('101.201.174.210')); echo $v->getBrowser(),PHP_EOL; echo $v->getFromPage(),PHP_EOL;
Rendering:
The above is the detailed content of Example analysis of how to obtain visitor information using PHP. For more information, please follow other related articles on the PHP Chinese website!