11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPでGeoIPを使う

Posted at

概要

GeoIPはIPアドレスから国を特定することができるPHPのライブラリ。
アクセス解析で役に立つ。
※CentOS 6.5を使用

pearのインストール

インストール済みなら、不要。

$ yum install php-pear

Net_GeoIPのインストール

$ pear install Net_GeoIP

データベースのダウンロード

$ wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip GeoIP.dat.gz

ソースコード

geoip.php
<?php
  require_once "Net/GeoIP.php";

  $geoip = Net_GeoIP::getInstance("./GeoIP.dat");

  try {
    echo $geoip->lookupCountryCode("1.161.19.32");
    // 結果 TW
  } catch (Exception $e) {
    // 例外処理
  }

ドキュメント

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?