<?php
function echoTrace ($s) {};
function getDistance($pta, $ptb) {
$earth_radius = 6378137; // Terre = sphère de 6378km de rayon
$dlo = ($ptb['lon'] - $pta['lon']) / 2.0;
$dla = ($ptb['lat'] - $pta['lat']) / 2.0;
$a = (sin($dla) * sin($dla)) + cos($pta['lat']) * cos($ptb['lat']) * (sin($dlo) * sin($dlo));
$d = 2 * atan2(sqrt($a), sqrt(1 - $a));
return ($earth_radius * $d);
}
function getRealIpAddr() {
$ip = '127.0.0.1';
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip=$_SERVER['HTTP_CLIENT_IP']; }
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; }
else { $ip=$_SERVER['REMOTE_ADDR']; }
return $ip;
}
function getIpLocation($ip) {
$se = & $_SESSION;
if (!array_key_exists('geoplugin', $se)) { $se['geoplugin'] = array(); }
if (array_key_exists($ip, $se['geoplugin'])) {
echoTrace ('from cache</br>');
$xml = $se['geoplugin'][$ip];
$record = $se['geoplugin'][$ip];
} else {
echoTrace ('from internet</br>');
$record = array();
$valid = filter_var($ip, FILTER_VALIDATE_IP);
if ($valid) {
$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=".$ip);
$record['countryCode'] = (string) $xml->geoplugin_countryCode;
$record['countryName'] = (string) $xml->geoplugin_countryName;
$record['latitude'] = (float) $xml->geoplugin_latitude;
$record['longitude'] = (float) $xml->geoplugin_longitude;
}
$se['geoplugin'][$ip] = $record;
}
return $record;
}
function getDistanceFromLoc($iploc, $lat, $long) {
$pta= [ 'lat' => deg2rad($lat), 'lon' => deg2rad($long) ];
$ptb= [ 'lat' => deg2rad($iploc['latitude']), 'lon' => deg2rad($iploc['longitude']) ];
return (int)(getDistance($pta, $ptb) / 1000);
}
function getIpLocation_($ip) {
$r = '';
$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=".$ip);
$r .= $xml->geoplugin_countryName ;
foreach ($xml as $key => $value) { $r .= $key." = ".$value. "</br>\n" ; }
return $r;
}
?>