===============================================
<?php
//获取访客ip
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']);
}
echo getIp();
?>
=========
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<?php
function get_env(){
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { if(is_ip($_SERVER['HTTP_X_FORWARDED_FOR'])) return $_SERVER['HTTP_X_FORWARDED_FOR']; $ip = trim(end(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']))); if(is_ip($ip)) return $ip; } if(isset($_SERVER['REMOTE_ADDR']) && is_ip($_SERVER['REMOTE_ADDR'])) return $_SERVER['REMOTE_ADDR']; if(isset($_SERVER['HTTP_CLIENT_IP']) && is_ip($_SERVER['HTTP_CLIENT_IP'])) return $_SERVER['HTTP_CLIENT_IP']; return '0.0.0.0';
}
function is_ip($ip) { return preg_match("/^([0-9]{1,3}.){3}[0-9]{1,3}$/", $ip); }
echo get_env();
?>
========================================================
<?php //这个类似用来获取访客信息的 //方便统计 header("Content-Type: text/html; charset=utf-8"); //获取访客ip 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获取城市、网络运营商等信息 function findCityByIp($ip){ $data = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip); return json_decode($data,$assoc=true); }
//获取用户浏览器类型 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'; }
//获取网站来源 function getFromPage(){ return $_SERVER['HTTP_REFERER']; }
echo $ip = getIp(); var_dump(findCityByIp($ip)['data']['city']); echo getBrowser();
|