php实现获取访客信息的实例分析
<?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;
效果图:
以上就是php实现获取访客信息的实例分析的详细内容,更多请关注php中文网其它相关文章!
已抢26913个
抢已抢14296个
抢已抢7179个
抢已抢8639个
抢已抢17629个
抢已抢7103个
抢