当前位置:网站首页>How PHP gets the user's City
How PHP gets the user's City
2022-06-25 04:49:00 【Oglie's magic slippers~】
php How to get the user's city
1, Scenario description
For example, on a page of the wechat Mall , Want to get the city of wechat users , So as to screen out some contents of this city .
2, First, we need to get the client ip Address ( Note that it is not a server ), Don't talk much , Post code
function getClientIP()
{
if (isset($_SERVER)) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
/* take X-Forwarded-For The first one is not unknown Effective IP character string */
foreach ($arr AS $ip) {
$ip = trim($ip);
if ($ip != 'unknown') {
$realip = $ip;
break;
}
}
if(!isset($realip)){
$realip = "0.0.0.0";
}
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$realip = $_SERVER['HTTP_CLIENT_IP'];
} else {
if (isset($_SERVER['REMOTE_ADDR'])) {
$realip = $_SERVER['REMOTE_ADDR'];
} else {
$realip = '0.0.0.0';
}
}
} else {
if (getenv('HTTP_X_FORWARDED_FOR')) {
$realip = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('HTTP_CLIENT_IP')) {
$realip = getenv('HTTP_CLIENT_IP');
} else {
$realip = getenv('REMOTE_ADDR');
}
}
preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
$realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
return $realip;
}
3, Then we can ip Get the address of the user's city , The code is as follows :
// Get the city
public function getCity()
{
// Get the city where the current location is located
$getIp = $this->getClientIP();
$content = file_get_contents("http://api.map.baidu.com/location/ip?ak=2TGbi6zzFm5rjYKqPPomh9GBwcgLW5sS&ip={
$getIp}&coor=bd09ll");
$json = json_decode($content);
$address = $json->{
'content'}->{
'address'};// Extraction by hierarchy address data
$data['address'] = $address;
$return['province'] = mb_substr($data['address'],0,3,'utf-8');
$return['city'] = mb_substr($data['address'],3,3,'utf-8');
return $return;
}
4, Successfully obtained the user's city ( r e t u r n [ ′ p r o v i n c e ′ ] by province Share , return['province'] Province , return[′province′] by province Share ,return[‘city’] For the city )
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Stupid little egg i」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/qq_33210743/article/details/78251936
边栏推荐
- Upgrade PHP to php7 X (III) failure of wechat payment callback
- [esp32 learning path 6 - Flash encryption]
- Separation of storage and computing in Dahua cloud native database
- 为什么TCP握手刚刚好是3次呢?
- [untitled]
- Machine learning deep learning -- Vectorization
- 重磅直播 | 相移法+多频外差之数学原理推导+实现
- Efficient NoSQL database service Amazon dynamodb experience sharing
- LabVIEW development gas regulator
- Use of deferred environment variable in gbase 8s
猜你喜欢

固态硬盘开盘数据恢复的方法

Successfully solved: selenium common. exceptions. TimeoutException: Message: timeout: Timed out receiving message from

【图像融合】基于matlab方向离散余弦变换和主成分分析图像融合【含Matlab源码 1907期】

leetcode1221. 分割平衡字符串

dotnet-exec 0.4.0 released

WPF uses Maui's self drawing logic

What if the desktop computer is not connected to WiFi

Sleep more, you can lose weight. According to the latest research from the University of Chicago, sleeping more than 1 hour a day is equivalent to eating less than one fried chicken leg

JS' sort() function

php开发支付宝支付功能之扫码支付流程图
随机推荐
File upload vulnerability shooting range upload labs learning (pass1-pass5)
[untitled]
第九章 APP项目测试(2) 测试工具
parallel recovery slave next change & parallel recovery push change
JS, BOM, DOM (VI)
【无标题】
PostgreSQL database Wal - RM_ HEAP_ ID logging action
《QDebug 2022年6月》
PHP encapsulates curl to send get and post request methods, and uses
Vscade setting clang format
EL & JSTL (XIII)
How to apply for software
Codeforces Round #802 (Div. 2) C D
Why does the SQL statement hit the index faster than it does not?
2.0SpingMVC使用RESTful
台式电脑连不上wifi怎么办
The consciousness of a programmer
固態硬盤開盤數據恢複的方法
[esp32 learning path 6 - Flash encryption]
《牛客刷verilog》Part I Verilog快速入门