Home > Article > Backend Development > Implementing sharing of IP and geographical location classes in PHP_PHP Tutorial
This article mainly introduces the implementation of obtaining IP and geographical location sharing in PHP. The geographical positioning in this article uses SINA Common interface, friends in need can refer to it
I found that there was an unfinished script before, so I simply expanded it.
Obtaining IP uses classic logic, and geolocation uses SINA's universal interface.
See the source code for usage details:
The code is as follows:
$_SERVER['HTTP_X_FORWARDED_FOR']&&strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'],'unknown')){
return$_SERVER['HTTP_X_FORWARDED_FOR'];
}elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&
strcasecmp($_SERVER['REMOTE_ADDR'],'unknown')){
return$_SERVER['REMOTE_ADDR'];
}
}
private functionipCURL($url)
{
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
ob_start();
curl_exec($ch);
curl_close($ch);
$result=ob_get_contents();
ob_end_clean();
return$result;
}
}
?>
'ONLYIP'=>true,'ECHO'=>true
true,'ECHO'=>true));?>
'ONLYIP'=>true,'FORMAT'=>'JSON','ECHO'=>true
true,'FORMAT'=>'JSON','ECHO'=>true));?>
'ECHO'=>true
true));?>
'FORMAT'=>'JSON','ECHO'=>true
'JSON','ECHO'=>true));?>