我们都知道php中有变量记录单独访问的客户端ip地址,但是如果我们给他加上时间戳和国家地区,就可以更清楚的了解和掌握,每天都有哪些人来访问你的网站了。这个帖子像你介绍一种基于geoip数据库的简单记录方式,本文基于ubuntu操作系统,windows可以如法炮制,希望对广大ecs站长有参考价值:
第一步,安装geoip数据库,geoip为一家国外网络供应商提供的数据库,有免费和付费,对于我们普通站长,免费的精度就够用了,而且每个月的第一个礼拜二,会定时更新,下图就是我下载7月3号最新的数据库:
第一小步,下载数据库:wget
http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
第二小步:解压数据库:gunzip -d GeoLiteCity.dat.gz
第三小步:移动到网站根目录:sudo mv GeoLiteCity.dat /var/www/nginx-default
第二小步:
安装头文件和调用数据库的php文件:
geoipcity.inc.php这个头文件可以从以下网址获得
http://svnweb.mageia.org/web/data/lib/MaxMind/GeoIP/geoipcity.inc.php?revision=1525&view=co
record_ip.php文件定义两个函数为打开数据库和解析数据库;
当然如果你觉得拷贝麻烦和容易出错,这两个文件也可以使用我的站点上的直接替换,地址:
http://relax.name/gipd.html
或者
http://relax.name/geoipphp.zip
然后在你的主页中加入如下语句就可以
<?php
//write visitor ip address
include("record_ip.php");
?>
record_ip.php文件内容:
<?php
require_once("geoipcity.inc.php");
//ini_set("memory_limit","96M");
//最简单的函数只要这一个
function get_ip_record($user_ip) {
global $path_visitor_maps;
//假定 GeoLiteCity.data 放在与此文件同一目录下
$gi = geoip_open_VMWO('GeoLiteCity.dat', VMWO_GEOIP_STANDARD);
$record = geoip_record_by_addr_VMWO($gi, "$user_ip");
geoip_close_VMWO($gi);
//你可以直接使用上面取出的 $record 数据
return $record;
}
function get_location_info($user_ip) {
$record = get_ip_record($user_ip);
//或者使用下面加工后的 $location_info
global $GEOIP_REGION_NAME;
$location_info = array(); // Create Result Array
$location_info['city_name'] = (isset($record->city)) ? $record->city : ''; //城市
$location_info['city_name'] = (isset($record->city)) ? $record->city : ''; //城市
$location_info['state_name'] = (isset($record->country_code) && isset($record->region)) //州名
? $GEOIP_REGION_NAME[$record->country_code][$record->region] : '';
$location_info['state_code'] = (isset($record->region)) ? strtoupper($record->region) : ''; //州代号
$location_info['country_name'] = (isset($record->country_name)) ? $record->country_name : '--'; //国家名
$location_info['country_code'] = (isset($record->country_code)) ? strtoupper($record->country_code) : '--'; //国家代号
$location_info['latitude'] = (isset($record->latitude)) ? $record->latitude : '0'; //维度
$location_info['longitude'] = (isset($record->longitude)) ? $record->longitude : '0'; //经度
//php 站点设置了 utf-8 字符集必要时进行转码
$charset = 'utf-8';
// this fixes accent characters on UTF-8, only when the blog charset is set to UTF-8
if ( strtolower($charset) == 'utf-8' && function_exists('utf8_encode') ) {
if ($location_info['city_name'] != '' ) {
$location_info['city_name'] = utf8_encode($location_info['city_name']);
}
if ($location_info['state_name'] != '') {
$location_info['state_name'] = utf8_encode($location_info['state_name']);
}
if ($location_info['country_name'] != '') {
$location_info['country_name'] = utf8_encode($location_info['country_name']);
}
}
return $location_info;
}
function recordip($user_ip) {
//查询一个 IP 测试下
$location = get_location_info($_SERVER['REMOTE_ADDR']);
$ip=$_SERVER['REMOTE_ADDR'];
$file=fopen("incomeip.txt","ab ") or exit("Unable to open file!");
$wdate=date(DATE_RFC822);
fwrite($file,$wdate.":".$ip.":".$location['country_name'].":".$location['city_name']."n");
fclose($file);
}
recordip($_SERVER['REMOTE_ADDR']);
?>
第三步,在网站根目录下新建一个空文件incomeip.txt,加入日程表,每天定时移除文件,重新记录,以免文件体积太大:
在/etc/crontab文件中,加入一项每天0点将incomeip.txt移为incomeip-today.txt,然后新建一个incomeip.txt
结果:
每天用ftp登陆服务器,下载incomeip-today.txt文件,将会看到如下结果:
Fri, 05 Jul 13 06:00:24 0800:14.215.117.86:China:Guangzhou
Fri, 05 Jul 13 11:00:36 0800:221.205.61.100:China:Taiyuan
Fri, 05 Jul 13 16:41:19 0800:58.40.91.180:China:Shanghai
。。。。。
虽说是英文字母,不过都是汉语拼音的城市名,大家应该都看得懂。
实践的过程参考了不少网络上的文章,比如下面这个:
参考:
http://unmi.cc/php-geolitecity-ip-location
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。