1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
$a=array();
// verification proxy
if ($_SERVER['REMOTE_PORT'] == 80) {
$ip_avant = $_SERVER["REMOTE_ADDR"];
//get localisation
$ip2country = @ip2country($ip_avant);
}
print_r($ip2country["city"] );
function ip2country($ipadress)
{
$surl = "http://whatismyipaddress.com/ip/". $ipadress;
$data = "LOOKUPADDRESS=" . $ipadress;
$geolocalisation = do_post_request($surl, $data);
preg_match("#<tr><th>City:</th><td>(.*?)</td></tr>#is", $geolocalisation, $city, PREG_OFFSET_CAPTURE);
$a = array ("city" => trim($city[1][0]), "lat" => trim($lat[1][0]), "lng" => trim($lng[1][0]));
return $a;
}
function do_post_request($url, $data, $optional_headers = null) {
$start = strpos($url,'//')+2;
$end = strpos($url,'/',$start);
$host = substr($url, $start, $end-$start);
$domain = substr($url,$end);
$fp = pfsockopen($host, 80);
if(!$fp) return null;
fputs ($fp,"POST $domain HTTP/1.1\n");
fputs ($fp,"Host: $host\n");
if ($optional_headers) {
fputs($fp, $optional_headers);
}
fputs ($fp,"Content-type: application/x-www-form-urlencoded\n");
fputs ($fp,"Content-length: ".strlen($data)."\n\n");
fputs ($fp,"$data\n\n");
$response = "";
while(!feof($fp)) {
$response .= fgets($fp, 1024);
}
fclose ($fp);
return $response;
} |