位置情報に応じて条件分岐するPHPスクリプト
ページを見ている人の位置情報に応じてURLの条件分岐をするスクリプトです。
ネットでぐぐるとブラウザの言語設定で条件分岐するスクリプトは数多く公開されていたのですが、位置情報に応じての条件分岐スクリプトがなかったので公開します。
ipinfodbに登録する
IPアドレスを取得して、ユーザーの位置情報を取得するAPIです。
Free Geolocation tools for IP Location, API, database and fraud detection tools.
» IPInfoDB | Free IP Address Geolocation Tools
ip2locationlite.class.phpをDLする
以下のリンクからPHPコードをDLできます。
ip2locationlite.class.php
日本とフィリピンで条件分岐するPHPスクリプト
include_once('ip2locationlite.class.php');
$ipLite = new ip2location_lite;
$ipLite->setKey('あなたのAPIキー');
$visitorGeolocation = $ipLite->getCountry($_SERVER['REMOTE_ADDR']);
if($visitorGeolocation["countryCode"] == 'JP'||$visitorGeolocation["countryName"] == 'Japan'){
$jp = "Japan";
}else{
$jp = "Another";
}
if($jp == "Another"){
header("Location: 英語サイトURL");
}
以上で完了!