<?php
include("city.inc");
include("region.php");

$gi = geoip_open("database.dat",GEOIP_STANDARD);
if (isset($_POST['ip']) && $_POST['ip'] != null)
{
	$ip = $_POST['ip'];
}
else
{
	$ip = getUserIP();
}

$record = geoip_record_by_addr($gi, $ip);

$city = $record->city;
$region = $GEOIP_REGION_NAME[$record->country_code][$record->region];
$country = $record->country_name;
$countrycode = $record->country_code;
$longitude = $record->longitude;
$latitude = $record->latitude;
$areacode = $record->areacode;
$dmacode = $record->dmacode;

geoip_close($gi);

function getUserIP()
{
    if (isset($_SERVER))
	{
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
            return $_SERVER["HTTP_X_FORWARDED_FOR"];
        
        if (isset($_SERVER["HTTP_CLIENT_IP"]))
            return $_SERVER["HTTP_CLIENT_IP"];

        return $_SERVER["REMOTE_ADDR"];
    }

    if (getenv('HTTP_X_FORWARDED_FOR'))
        return getenv('HTTP_X_FORWARDED_FOR');

    if (getenv('HTTP_CLIENT_IP'))
        return getenv('HTTP_CLIENT_IP');

    return getenv('REMOTE_ADDR');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>IP Location Script</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body onload="setDurationPrice();">
<h2 style="text-align:center">IP Location Script</h2>
	<table width="1200" border="0" align="center" cellpadding="3" cellspacing="0" style="width:800px">
  <tr>
			<th style="text-align:right; width:50%">
				Your IP Address is:
			</td>
			<td>
				<?php echo $ip; ?>			</td>
		</tr>
		<tr>
			<th style="text-align:right; vertical-align:top">
				Dear Visitor from:
			</td>
			<td>
				<?php
					echo "City: ".$city.", Region: ".$region.", Country: ".$country;?>			</td>
		</tr><td style="text-align:right">
				City:
			</td>
			<td>
				<?php echo $city; ?>			</td>
		</tr>
		<tr>
			<td style="text-align:right">
				State / Province / Region:			</td>
			<td>
				<?php echo $region; ?>			</td>
		</tr>
		<tr>
			<td style="text-align:right">
				Country:			</td>
			<td>
				<?php echo $country; ?>			</td>
		</tr>
		<tr>
			<td style="text-align:right">
				Short country code:			</td>
			<td>
				<?php echo $countrycode; ?>			</td>
		</tr>
		<tr>
			<td style="text-align:right">
				Longitude:			</td>
			<td>
				<?php echo $longitude; ?>			</td>
		</tr>
		<tr>
			<td style="text-align:right">
				Latitude:			</td>
			<td>
				<?php echo $latitude; ?>			<?php echo $areacode; ?> <?php echo $dmacode; ?> <?php echo $org; ?> <?php echo $isp; ?></td>
	</table>
</body>
</html>