AkbarNSS.Com - Skrip PHP Untuk Mengetahui Negara Pengunjung berdasarkan IP address, skrip ini mendeteksi negara berdasarkan ip address, sangat berguna jika kita ingin menampilkan atau menyembunyikan konten dari negara yang di tujukan atau meredirect sebuah halaman ke halaman lain.
Skrip PHP Check Negara Visitor
KLIK VIEW RAW Agar bisa mengcopy CODEnya
![]() |
Cara Mengetahui Negera Pengunjung dengan PHP |
Skrip PHP Check Negara Visitor
Skrip ini sangat berguna bagi anda yang menekuni CPA, CPA adalah singkatan dari cost per action atau cost per acquisition, dan tentunya CPA Marketing itu sendiri masih berkaitan dengan Affiliate Marketing yang dimana kita akan mendapatkan bayaran/komisi apabila seseorang (leads) melakukan apa yang akan kita suruh lakukan di dalam halaman penawaran kita (landing page).
Tidak semua negara bisa menghasilkan lead, bahkan ada beberapa negara yang di blok dari offer tersebut, saya tidak membahas masalah cpa karena kapasitas saya belum cukup untuk berbagi pengalaman di CPA, namun jika anda pemain CPA khususnya movie mungkin artikel source landing page yang pernah saya tulis bisa di coba.
Mendapatkan IP address
Yang pertama adalah kita harus menentukan ip address, bisa menggunakan srip berikut ini
KLIK VIEW RAW Agar bisa mengcopy CODEnya
KLIK VIEW RAW Agar bisa mengcopy CODEnya
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_ip_address() { | |
$ip_keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR'); | |
foreach ($ip_keys as $key) { | |
if (array_key_exists($key, $_SERVER) === true) { | |
foreach (explode(',', $_SERVER[$key]) as $ip) { | |
// trim for safety measures | |
$ip = trim($ip); | |
// attempt to validate IP | |
if (validate_ip($ip)) { | |
return $ip; | |
} | |
} | |
} | |
} | |
return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false; | |
} | |
/** | |
* Ensures an ip address is both a valid IP and does not fall within | |
* a private network range. | |
*/ | |
function validate_ip($ip) | |
{ | |
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) { | |
return false; | |
} | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Retrieves the best guess of the client's actual IP address. | |
* Takes into account numerous HTTP proxy headers due to variations | |
* in how different ISPs handle IP addresses in headers between hops. | |
*/ | |
function get_ip_address() { | |
// check for shared internet/ISP IP | |
if (!empty($_SERVER['HTTP_CLIENT_IP']) && validate_ip($_SERVER['HTTP_CLIENT_IP'])) { | |
return $_SERVER['HTTP_CLIENT_IP']; | |
} | |
// check for IPs passing through proxies | |
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { | |
// check if multiple ips exist in var | |
if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') !== false) { | |
$iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); | |
foreach ($iplist as $ip) { | |
if (validate_ip($ip)) | |
return $ip; | |
} | |
} else { | |
if (validate_ip($_SERVER['HTTP_X_FORWARDED_FOR'])) | |
return $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} | |
} | |
if (!empty($_SERVER['HTTP_X_FORWARDED']) && validate_ip($_SERVER['HTTP_X_FORWARDED'])) | |
return $_SERVER['HTTP_X_FORWARDED']; | |
if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) | |
return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP']; | |
if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && validate_ip($_SERVER['HTTP_FORWARDED_FOR'])) | |
return $_SERVER['HTTP_FORWARDED_FOR']; | |
if (!empty($_SERVER['HTTP_FORWARDED']) && validate_ip($_SERVER['HTTP_FORWARDED'])) | |
return $_SERVER['HTTP_FORWARDED']; | |
// return unreliable ip since all else failed | |
return $_SERVER['REMOTE_ADDR']; | |
} | |
/** | |
* Ensures an ip address is both a valid IP and does not fall within | |
* a private network range. | |
*/ | |
function validate_ip($ip) { | |
if (strtolower($ip) === 'unknown') | |
return false; | |
// generate ipv4 network address | |
$ip = ip2long($ip); | |
// if the ip is set and not equivalent to 255.255.255.255 | |
if ($ip !== false && $ip !== -1) { | |
// make sure to get unsigned long representation of ip | |
// due to discrepancies between 32 and 64 bit OSes and | |
// signed numbers (ints default to signed in PHP) | |
$ip = sprintf('%u', $ip); | |
// do private network range checking | |
if ($ip >= 0 && $ip <= 50331647) return false; | |
if ($ip >= 167772160 && $ip <= 184549375) return false; | |
if ($ip >= 2130706432 && $ip <= 2147483647) return false; | |
if ($ip >= 2851995648 && $ip <= 2852061183) return false; | |
if ($ip >= 2886729728 && $ip <= 2887778303) return false; | |
if ($ip >;= 3221225984 && $ip <= 3221226239) return false; | |
if ($ip >;= 3232235520 && $ip <= 3232301055) return false; | |
if ($ip >= 4294967040) return false; | |
} | |
return true; | |
} |
Mendapatkan Nama Negara Berdasarkan IP address
Setelah mendapatkan IP address, kita bisa menggunakan salah satu skrip geoip, contohnya maxmind/geoip-api-phpCara Mendapatkan Nama Negara Berdasarkan IP
Setelah medapatka dua function di atas kita bisa mendapatkan nama negara dari sebuah IP address, caranya seperti ini.- Pertama Download geoIP by Maxmind
- Buat File index.php, paste kode di bawah ini
- Sesuaikan letak file yang di include
KLIK VIEW RAW Agar bisa mengcopy CODEnya
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//https://github.com/maxmind/geoip-api-php | |
//https://gist.github.com/cballou/2201933 | |
//sedotcode.blogspot.com | |
include("../src/geoip.inc"); | |
function get_ip_address() { | |
$ip_keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR'); | |
foreach ($ip_keys as $key) { | |
if (array_key_exists($key, $_SERVER) === true) { | |
foreach (explode(',', $_SERVER[$key]) as $ip) { | |
// trim for safety measures | |
$ip = trim($ip); | |
// attempt to validate IP | |
if (validate_ip($ip)) { | |
return $ip; | |
} | |
} | |
} | |
} | |
return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false; | |
} | |
/** | |
* Ensures an ip address is both a valid IP and does not fall within | |
* a private network range. | |
*/ | |
function validate_ip($ip) | |
{ | |
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) { | |
return false; | |
} | |
return true; | |
} | |
// Buang komen dibawah ini untuk mendapatkan GeoIP/Lite City. | |
// include("geoipcity.inc"); | |
//Sesuaikan dengan lokasi file GeoIP.dat | |
$gi = geoip_open("../include/GeoIP.dat", GEOIP_STANDARD); | |
echo geoip_country_code_by_addr($gi, get_ip_address()) . "<br>" ; | |
echo geoip_country_code_by_addr($gi, get_ip_address()) . "<br>" ; | |
geoip_close($gi); | |
?> |