The stupidest and best way to enhance the memory of knowledge is to take notes. Starting from today, I will record my PHP work and study notes.
1. Obtain the visitor’s IP address and city code
The first is the function to obtain the visitor’s IP address
function getRealIp() {
If (!emptyempty($_SERVER['HTTP_CLIENT_IP'])) { //check ip from share internet
$ip=$_SERVER['HTTP_CLIENT_IP'];
} elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip=$_SERVER['REMOTE_ADDR'];
}
Return $ip;
}
After obtaining the IP address, access an API provided by www.ip138.com to obtain the city
function ips($ip){
$str=file_get_contents("http://www.ip138.com/ips.asp?ip={$ip}&action=2");
Preg_match("/
/",$str,$m);
$pstr=str_replace("","",$m[1]);
$arr=explode("
",$pstr);
Array_shift($arr);
Return $arr;
}
2. Obtain the IP and city through the API provided by Tencent (note: only the address of the server can be obtained, and it is a pity to obtain the visitor's information).
Personally, I think this function is very good, but unfortunately it only obtains the information about the server location.
function get_ip_place(){
$ip=file_get_contents("http://fw.qq.com/ipaddress");
$ip=str_replace('"',' ',$ip);
$ip2=explode("(",$ip);
$a=substr($ip2[1],0,-2);
$b=explode(",",$a);
return $b;
}
$ip=get_ip_place();
print_r($ip);
Basic knowledge supplement:
$_SERVER['HTTP_CLIENT_IP']: Get the proxy ip (may exist and can be forged)
$_SERVER['HTTP_X_FORWARDED_FOR']: Which IP the user is using as a proxy (it may exist, or it can be forged. The real IP address obtained by some function is obtained by using this common variable)
$_SERVER['REMOTE_NAME']: Access terminal (may be a user, may be a proxy) IP
Author "volley"
http://www.bkjia.com/PHPjc/478646.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478646.htmlTechArticleThe stupidest and best way to enhance the memory of knowledge is to take notes. Starting today, I will record my PHP work and study notes. 1. Obtain the visitor’s IP address and city code...