很多站长朋友们都不太清楚phpip定位城市,今天小编就来给大家整理phpip定位城市,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php根据ip地址查地区 2、 php 怎么通过ip来获取所在位置 3、 如何进行PHP查询ip所在地 php根据ip地址查地区自己以前做过一个程序 根据discuz里面的ip查询改的
/**
* ip地址所属地区计算
* 修改自 discuz
* 使用dicuz tinyipdata数据文件
* 将一些英文提示修改为汉字
* $is_simple true的话显示到市 false显示到网通电信等等
******/
function convertip($ip,$is_simple=true,$ipfile='include/data/ip.dat') {
$return = '';
if( !file_exists($ipfile) ) $ipfile = ''.$ipfile;
if(preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip)) {
$iparray = explode('.', $ip);
if($iparray[0] == 10 || $iparray[0] == 127 || ($iparray[0] == 192 $iparray[1] == 168) || ($iparray[0] == 172 ($iparray[1] >= 16 $iparray[1] <= 31))) {
return '局域网';
} elseif($iparray[0] > 255 || $iparray[1] > 255 || $iparray[2] > 255 || $iparray[3] > 255) {
return 'ERR';
} elseif($is_simple) {
return change_simply_area(convertip_tiny($ip, $ipfile));
}
else {
return convertip_tiny($ip, $ipfile);
}
}
}
/**
* 从ip文件得到ip所属地区
*
* 过滤掉了具体的位置(如 网通/电信/**网吧) 基本到市
***/
function convertip_tiny($ip, $ipdatafile) {
static $fp = NULL, $offset = array(), $index = NULL;
$ipdot = explode('.', $ip);
$ip = pack('N', ip2long($ip));
$ipdot[0] = (int)$ipdot[0];
$ipdot[1] = (int)$ipdot[1];
if($fp === NULL $fp = @fopen($ipdatafile, 'rb')) {
$offset = unpack('Nlen', fread($fp, 4));
$index = fread($fp, $offset['len'] - 4);
} elseif($fp == FALSE) {
return '- Invalid IP data file';
}
$length = $offset['len'] - 1028;
$start = unpack('Vlen', $index[$ipdot[0] * 4] . $index[$ipdot[0] * 4 + 1] . $index[$ipdot[0] * 4 + 2] . $index[$ipdot[0] * 4 + 3]);
for ($start = $start['len'] * 8 + 1024; $start < $length; $start += 8) {
if ($index{$start} . $index{$start + 1} . $index{$start + 2} . $index{$start + 3} >= $ip) {
$index_offset = unpack('Vlen', $index{$start + 4} . $index{$start + 5} . $index{$start + 6} . "\x0");
$index_length = unpack('Clen', $index{$start + 7});
break;
}
}
fseek($fp, $offset['len'] + $index_offset['len'] - 1024);
if($index_length['len']) {
return mb_convert_encoding(fread($fp, $index_length['len']),'utf-8','gb2312'); //将读出的gb编码数据转成utf-8并返回
} else {
return '未知';
}
}
function change_simply_area($area) {
$tmp = explode(' ',$area); //过滤掉一些具体信息
return $tmp[0];
}
里面那个ipfile你可以去下载一个discuz 在 ipdata目录里面有wry.dat的文件就是了 其实这个就是网上用的最多的那个纯真版数据库 很多显示ip的qq用的也是那个
这两个函数的原型参考 discuz 里面 include\misc.func.php
共同学习进步 :)
php 怎么通过ip来获取所在位置echo getcposition(getIP());
function getIP(){
if (isset($_SERVER)) {
if (isset($_SERVER[HTTP_X_FORWARDED_FOR])) {
$realip = $_SERVER[HTTP_X_FORWARDED_FOR];
} elseif (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");
} elseif (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
function getcposition($ip){
$res1 = file_get_contents("");
$res1 = json_decode($res1,true);
//print_r($res1);
if ($res1[ "code"]==0){
return $res1['data']["country"].$res1['data'][ "region"].$res1['data']["city"]."_".$res1['data'][ "isp"];
}else{
return "未知";
}
}
?>
如何进行PHP查询ip所在地<?php
/**
* 根据IP地址取得地理位置
*/
function get_ip_arr()
{
$ip=file_get_contents("");
preg_match_all("/\"(.*)\"/",$ip,$arr);
return $arr;
}
//返回一个数组,包括地区、IP等信息,自行获取即可。
?>
关于phpip定位城市的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于phpip定位城市 php获取地理位置的详细内容...