Home > Backend Development > PHP Tutorial > php 如何获得客户端的ip 和所在省市

php 如何获得客户端的ip 和所在省市

WBOY
Release: 2016-06-13 10:07:04
Original
1016 people have browsed it

php 怎么获得客户端的ip 和所在省市
求具体实现代码。。谢谢。。

------解决方案--------------------

提供几个ip地址API
 
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
新浪另外一个IP地址查询接口:http://counter.sina.com.cn/ip
新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42 
搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson 
搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8 
搜狐另外的IP地址查询接口:http://txt.go.sohu.com/ip/soip
------解决方案--------------------
1.可以自己建ip库,数据可以从类似纯真之类的数据包里提取

2.获取ip的代码 网上一大堆 这里复制一个

function getip()
{
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
{
$ip = getenv("HTTP_CLIENT_IP");
} else
if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"),
"unknown"))
{
$ip = getenv("HTTP_X_FORWARDED_FOR");
} else
if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
{
$ip = getenv("REMOTE_ADDR");
} else
if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'],
"unknown"))
{
$ip = $_SERVER['REMOTE_ADDR'];
} else
{
$ip = "unknown";
}
return ($ip);
}

3.可以将ip传递给一些目前网上可用的接口取得返回的数据进行解析即可

例如
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=219.136.252.33

注意不传递ip的话自动获取访问者的ip地址.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template