1获取真实ip,本地测试总是::1 或者127.0.0.1 或者局域网的ip

/**
* 获取用户真实 IP
*/
function getIP()
{
static $realip;
if (isset($_SERVER)){
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
$realip = $_SERVER["HTTP_CLIENT_IP"];
} else {
$realip = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")){
$realip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}

2.根据ip获取地址

/**
* 获取 IP 地理位置
* 淘宝IP接口
* @Return: array
*/
function getCity($ip = '')
{
if($ip == ''){
$url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
$ip=json_decode(file_get_contents($url),true);
$data = $ip;
}else{
$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
$ip=json_decode(file_get_contents($url));
if((string)$ip->code=='1'){
return false;
}
$data = (array)$ip->data;
} return $data;
}

测试

var_dump(getIP());
var_dump(getCity());
var_dump(getCity('218.93.250.162'));

结果

string(3) "::1"
array(10) {
["ret"]=>
int(1)
["start"]=>
int(-1)
["end"]=>
int(-1)
["country"]=>
string(6) "中国"
["province"]=>
string(6) "江苏"
["city"]=>
string(6) "宿迁"
["district"]=>
string(0) ""
["isp"]=>
string(0) ""
["type"]=>
string(0) ""
["desc"]=>
string(0) ""
}
array(13) {
["country"]=>
string(6) "中国"
["country_id"]=>
string(2) "CN"
["area"]=>
string(6) "华东"
["area_id"]=>
string(6) "300000"
["region"]=>
string(9) "江苏省"
["region_id"]=>
string(6) "320000"
["city"]=>
string(9) "宿迁市"
["city_id"]=>
string(6) "321300"
["county"]=>
string(0) ""
["county_id"]=>
string(2) "-1"
["isp"]=>
string(6) "电信"
["isp_id"]=>
string(6) "100017"
["ip"]=>
string(14) "218.93.250.162"
}

说明:新浪的接口,直接能获取到地址信息,淘宝的接口需要提供ip,不过获取的信息更全面

转载:https://www.cnblogs.com/jiqing9006/p/4994728.html#commentform

PHP获取ip与ip所在城市的更多相关文章

  1. php 获取客户端IP地址经纬度所在城市

    1. [代码]获取客户端IP地址经纬度所在城市 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php   $getIp=$_SERVER["REMOTE_ADDR ...

  2. 根据IP定位用户所在城市信息

    http://www.9958.pw/post/city_ip 1.调用新浪IP地址库 新浪提供了开放的IP地址库数据供开发者调用,调用地址: http://int.dpool.sina.com.cn ...

  3. python中通过客户端IP拿到所在城市和当地天气信息—附带项目案例

    熟悉老一代QQ的小伙伴可能都知道,很早以前的QQ,鼠标滑到头像的位置,你的位置和IP会在详情页显示,那么这个是如何做到的呢?下面我们就来玩一玩这个东西 首先,需求分析: 1.拿到客户端IP 2.通过I ...

  4. 获取客户端IP地址经纬度所在城市

    <?php $getIp=$_SERVER["REMOTE_ADDR"]; echo 'IP:',$getIp; echo '<br/>'; $content = ...

  5. PHP中根据IP地址判断所在城市等信息

    本篇文章由:http://xinpure.com/php-based-on-information-such-as-the-ip-address-in-your-city/ 获得IP地址 在 PHP ...

  6. 通过IP来判断所在城市

    1 今天的讲解什么? 如何根据ip查询出所在城市?我把博客园中收集的教程整理了一下,主要结合调用相关API,或者通过纯真数据库来解决这个问题. 2 相关介绍 2.1 这个是什么? ​通过IP查询所在城 ...

  7. C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市

    百度天气 接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsC ...

  8. PHP:根据IP地址获取所在城市

    文件目录: ipLocation -----qqwry ----------QQWry.Dat -----ipCity.class.php ipCity.class.php文件代码: <?php ...

  9. php根据IP获取IP所在城市

    转载出处:php实现根据IP地址获取其所在省市的方法 //根据现有IP地址获取其地理位置(省份,城市等)的方法 function GetIpLookup($ip = ''){ if(empty($ip ...

  10. ip获取所在城市名称等信息接口,及函数

    函数: 1 2 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 ...

随机推荐

  1. 1406: [AHOI2007]密码箱

    1406: [AHOI2007]密码箱 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1591  Solved: 944[Submit][Status][ ...

  2. 【题解】CQOI2012局部最小值

    上课讲的一道题,感觉也挺厉害的~正解是容斥 + 状压dp.首先我们容易发现一共可能的局部最小值数量是十分有限的,最多也只有 \(8\) 个.所以我们可以考虑状压. 建立出状态 \(f[i][j]\) ...

  3. POJ1204:Word Puzzles——题解

    http://poj.org/problem?id=1204 题目大意:给一个字母表,求一些字符串的开端第一次出现的位置和字符串的方向(字符串可以按照八个方向放在字母表中可匹配的位置) ——————— ...

  4. Python 编程实战提高测试工作效率实例之svn 文件管理

    #coding=utf-8 ''' Created on 2016年8月22日 @author:Tom Gao ''' importre importos importtime "" ...

  5. Codechef MARCH14 GERALD07加强版

    强制在线不代表不能预处理! 考虑暴力怎么干? 开始n个联通块.now=n 不断加入边,如果连接两个联通块,--now 否则不动. 后者的前提是和[l,id-1]的边构成环 所以,我们考虑每个[l,r] ...

  6. 001 Python中的变量和字符串

    1.Python“变量”更像“名字” 变量名就像我们现实社会的名字,把一个值赋值给一个名字时,Ta会存储在内存中,称之为变量(variable). 在大多数语言中,都把这种行为称为“给变量赋值”或“把 ...

  7. How to configue session timeout in Hive

    This article explains how to configure the following settings in Hive:hive.server2.session.check.int ...

  8. sun.security.x509.CertAndKeyGen;找不到

    导入已有项目编译时出错,报: import sun.security.x509.CertAndKeyGen;找不到 而这个包属于sun公司的jar包.不是项目本身的问题,而是开发环境的问题. 最后原因 ...

  9. 问题BeanFactory not initialized or already closed - call 'refresh' before access

    问题BeanFactory not initialized or already closed - call 'refresh' before access 2016-08-23 14:22 8565 ...

  10. 洛谷2944 [USACO09MAR]地震损失2Earthquake Damage 2

    https://www.luogu.org/problem/show?pid=2944 题目描述 Wisconsin has had an earthquake that has struck Far ...