Anda di halaman 1dari 3

<?

php

$conn = mysql_connect(nama_host, user, password);


if(! $conn )
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db( db );

$sqlt = "CREATE TABLE ip_geo( ".


"ip VARCHAR(100) NOT NULL , ".
"city VARCHAR(100) NOT NULL , ".
"country VARCHAR(100) NOT NULL , ".
"countryCode VARCHAR(100) NOT NULL , ".
"PRIMARY KEY ( ip )); ";
mysql_select_db( db );

$ipaddr= str_replace('for=','',$_SERVER['REMOTE_ADDR']);
//echo $ipaddr;
$inp=explode(".",$ipaddr);
//print_r($inp);
if(count($inp)>3){
$ipop="";
for( $n=0; $n<3 ; $n++){
$ipop =$ipop.$inp[$n].".";

}
$mainip= $ipop."0";
}else{

$mainip= $ipaddr;
}
$query = mysql_query("SELECT * FROM ip_geo WHERE ip = '$mainip'");
if(!$query)
{
$buattabel = mysql_query( $sqlt, $conn );
if(! $buattabel )
{
die('Gagal Membuat Tabel: ' . mysql_error());
}
}
if(mysql_num_rows($query) <> 0) {
$hasil = mysql_fetch_array($query);
$ip=$hasil['ip'];
$city=$hasil['city'];
$country=$hasil['country'];
$countryCode=$hasil['countryCode'];

}else{

$ch = curl_init("http://ip-api.com/json/".$mainip);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$jsonip = curl_exec($ch);
curl_close($ch);
$ipinfo=json_decode(utf8_encode($jsonip),true);
if(!empty($ipinfo))
{
$countryCode=$ipinfo['countryCode'];
$query = "INSERT INTO ip_geo (ip, city,country,countryCode) VALUES ('".
$mainip."','".str_replace("'","\'",$ipinfo['city'])."','".str_replace("'","\'",
$ipinfo['country'])."','".$ipinfo['countryCode']."')";
$ins=mysql_query($query,$conn);

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;
}
?>