JABZ7SHOP : Web Selling Things With Cheap Price : https://jabz7shop.blogspot.com/ : Go To My Shop Online Now
Bài đăng

Share code PHP nhận diện mã vùng theo ip - Detect Location Country from IP

2 min read

 Nếu bạn đang tìm code có thể nhận diện người dùng ở nước nào thì tìm đúng rồi đấy ^^

Đầu tiên tạo file geo.php với nội dung như sau:

www.geoplugin.net/json.gp?ip=

<?php
class geoPlugin {
	
	//geoPlugin server
	var $host = 'http://www.geoplugin.net/php.gp?ip={IP}&base_currency={CURRENCY}&lang={LANG}';
		
	//đơn vị tiền tệ mặc định
	var $currency = 'USD';
	
	//ngôn ngữ mặc định
	var $lang = 'en';
	var $ip = null;
	var $city = null;
	var $region = null;
	var $regionCode = null;
	var $regionName = null;
	var $dmaCode = null;
	var $countryCode = null;
	var $countryName = null;
	var $inEU = null;
	var $euVATrate = false;
	var $continentCode = null;
	var $continentName = null;
	var $latitude = null;
	var $longitude = null;
	var $locationAccuracyRadius = null;
	var $timezone = null;
	var $currencyCode = null;
	var $currencySymbol = null;
	var $currencyConverter = null;
	
	function __construct() {

	}
	
	function locate($ip = null) {
		
		global $_SERVER;
		
		if ( is_null( $ip ) ) {
			$ip = $_SERVER['REMOTE_ADDR'];
		}
		
		$host = str_replace( '{IP}', $ip, $this->host );
		$host = str_replace( '{CURRENCY}', $this->currency, $host );
		$host = str_replace( '{LANG}', $this->lang, $host );
		$data = array();
		$response = $this->fetch($host);
		$data = unserialize($response);

		$this->ip = $ip;
		$this->city = $data['geoplugin_city'];
		$this->region = $data['geoplugin_region'];
		$this->regionCode = $data['geoplugin_regionCode'];
		$this->regionName = $data['geoplugin_regionName'];
		$this->dmaCode = $data['geoplugin_dmaCode'];
		$this->countryCode = $data['geoplugin_countryCode'];
		$this->countryName = $data['geoplugin_countryName'];
		$this->inEU = $data['geoplugin_inEU'];
		$this->euVATrate = $data['geoplugin_euVATrate'];
		$this->continentCode = $data['geoplugin_continentCode'];
		$this->continentName = $data['geoplugin_continentName'];
		$this->latitude = $data['geoplugin_latitude'];
		$this->longitude = $data['geoplugin_longitude'];
		$this->locationAccuracyRadius = $data['geoplugin_locationAccuracyRadius'];
		$this->timezone = $data['geoplugin_timezone'];
		$this->currencyCode = $data['geoplugin_currencyCode'];
		$this->currencySymbol = $data['geoplugin_currencySymbol'];
		$this->currencyConverter = $data['geoplugin_currencyConverter'];
	}
	function fetch($host) {
		if ( function_exists('curl_init') ) {
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $host);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.1 Share by LYT');
			$response = curl_exec($ch);
			curl_close ($ch);
			
		} else if ( ini_get('allow_url_fopen') ) {
			$response = file_get_contents($host, 'r');
			
		} else {

			trigger_error ('Lỗi GeoPlugin: Không thể truy xuất dữ liệu. PHP không hỗ trợ cURL hoặc chưa bật allow_url_fopen trong php.ini', E_USER_ERROR);
			return;
		
		}
		
		return $response;
	}

	function convert($amount, $float=2, $symbol=true) {
		if ( !is_numeric($this->currencyConverter) || $this->currencyConverter == 0 ) {
			trigger_error('geoPlugin: currencyConverter không có giá trị nào.', E_USER_NOTICE);
			return $amount;
		}
		if ( !is_numeric($amount) ) {
			trigger_error ('geoPlugin: giá trị geoPlugin::convert không phải là số.', E_USER_WARNING);
			return $amount;
		}
		if ( $symbol === true ) {
			return $this->currencySymbol . round( ($amount * $this->currencyConverter), $float );
		} else {
			return round( ($amount * $this->currencyConverter), $float );
		}
	}

	function nearby($radius=10, $limit=null) {
		if ( !is_numeric($this->latitude) || !is_numeric($this->longitude)) {
			trigger_error ('geoPlugin: không thể xác định được tọa độ.', E_USER_NOTICE);
			return array( array() );
		}
		$host = "http://www.geoplugin.net/extras/nearby.gp?lat=" . $this->latitude . "&long=" . $this->longitude . "&radius={$radius}";
		if (is_numeric($limit))$host .= "&limit={$limit}";
		return unserialize( $this->fetch($host) );
	}
}
?>

Bên trên là api mà chúng ta sẽ get được thông tin của người dùng như ip,city, region, country các thứ blabla
Tiếp theo muốn hiển thị lấy thông tin ở đâu thì thêm đoạn code sau vào nơi muốn hiện:

<?php
require_once('geo.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
// tạo một biến cho mã quốc gia
$var_country_code = $geoplugin->countryCode;
// xuất mã quốc gia
echo $var_country_code;
?>

Ở đây mình ví dụ nếu người dùng là Việt Nam thì sẽ redirect vào ngôn ngữ Việt:

<?php
require_once('geo.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
// tạo một biến cho mã quốc gia
$var_country_code = $geoplugin->countryCode;
// nếu là ip việt thì redirect tới wiki tiếng việt (mặc định là en)
// danh sách mã vùng xem tại https://www.geoplugin.com/iso3166
if ($var_country_code == "VN") {
header('Location: http://vi.wikipedia.org/');
} else {
header('Location: http://en.wikipedia.org/');
}
?>

Demo
Danh sách mã vùng xem tại đây
Nếu muốn lấy dữ liệu khác thì các bạn soi code ở trên là được 😶
API geoPlugin, Sample code by LYT

00:00 / 00:00

Bạn có thể thích những bài đăng này

Đăng nhận xét