PHP 教程:php 根据 ip 查询所在地区
关于 php 根据 ip 查询所在地区的实现方法,需要的朋友参考下
dat 文件,关于 ip 对应地区的信息文件
qqwry.dat 文件
网上自己下载
class
类文件,解析 qqwry.data 文件的
IpLocation.php 文件
复制代码 代码如下:
<?php
class
IpLocation {
/**
* @var resource 指针
*/
private
$fp
;
/**
* 第一条 IP 记录的偏移地址
* @var int
*/
private
$firstip
;
/**
* 最后一条 IP 记录的偏移地址
* @var int
*/
private
$lastip
;
/**
* IP 记录的总条数(不包含版本信息记录)
* @var int
*/
private
$totalip
;
/**
*
构造函数,打开 QQWry.Dat 文件并初始化类中的信息
* @param string $filename
* @return IpLocation
*/
public
function
__construct(
$filename
= "qqwry.dat") {
$this
->fp = 0;
if
((
$this
->fp = @
fopen
(
$filename
, 'rb')) !== false) {
$this
->firstip =
$this
->getlong();
$this
->lastip =
$this
->getlong();