tools/.hta_slug/location-ip-to-location.php

118 lines
7.3 KiB
PHP

<?php
require '.hta_lib/vendors/maxmind-db-reader/vendor/autoload.php';
?>
<section class="diZContainer diZmxAuto diZmy8">
<h1 class="diZBorderBottom">IP Information</h1>
<p>This tool provides detailed geographical and ISP information for a given IP address using the GeoIP2.</p>
<?php
use GeoIp2\Database\Reader;
if (isset($_GET['ip'])) {
try {
$reader = new Reader('.hta_lib/data/GeoLite2-City.mmdb');
$reader2 = new Reader('.hta_lib/data/GeoLite2-ASN.mmdb');
$records = $reader->city($_GET['ip']);
$records2 = $reader2->asn($_GET['ip']);
// var_dump($records2);
$country = $records->country->name ?? null;
$state = $records->mostSpecificSubdivision->name ?? null;
$city = $records->city->name ?? null;
$region = $records->subdivisions[0]->name ?? null;
$timezone = $records->location->timeZone ?? null;
$data = [
'ip' => $_GET['ip'],
'country' => $records->country->name ?? null,
'country_iso_code' => $records->country->iso_code ?? null,
'state' => $records->mostSpecificSubdivision->name ?? null,
'state_iso_code' => $records->mostSpecificSubdivision->iso_code ?? null,
'city' => $records->city->name ?? null,
'region' => $records->subdivisions[0]->name ?? null,
'timezone' => $records->location->timeZone ?? null,
'latitude' => $records->location->latitude ?? null,
'longitude' => $records->location->longitude ?? null,
'postal_code' => $records->postal->code ?? null,
'continent' => $records->continent->name ?? null,
'asn' => $records2->autonomousSystemNumber ?? null,
'isp' => $records2->autonomousSystemOrganization ?? null,
];
$dataArray = [$data]; // Wrap in an array if it's a single record.
?>
<div class="diZGridCols1-3">
<?php
foreach ($dataArray as $record) { // Ensure you loop through an array
if (is_array($record)) { // Check if record is an array
?>
<h3 style="border-bottom: 1px solid #808080;"><strong>IP:</strong> <?php echo $record['ip']; ?></h3>
<h3 style="border-bottom: 1px solid #808080;"><strong>Continent:</strong> <?php echo $record['continent']; ?></h3>
<h3 style="border-bottom: 1px solid #808080;"><strong>Country:</strong> <?php echo $record['country']; ?></h3>
<h3 style="border-bottom: 1px solid #808080;"><strong>State:</strong> <?php echo $record['state']; ?></h3>
<h3 style="border-bottom: 1px solid #808080;"><strong>City:</strong> <?php echo $record['city']; ?></h3>
<h3 style="border-bottom: 1px solid #808080;"><strong>Region:</strong> <?php echo $record['region']; ?></h3>
<h3 style="border-bottom: 1px solid #808080;"><strong>Timezone:</strong> <?php echo $record['timezone']; ?></h3>
<h3 style="border-bottom: 1px solid #808080;"><strong>Latitude:</strong> <?php echo $record['latitude']; ?></h3>
<h3 style="border-bottom: 1px solid #808080;"><strong>Longitude:</strong> <?php echo $record['longitude']; ?></h3>
<h3 style="border-bottom: 1px solid #808080;"><strong>Postal Code:</strong> <?php echo $record['postal_code']; ?></h3>
<h3 style="border-bottom: 1px solid #808080;"><strong>ASN:</strong> <?php echo $record['asn']; ?></h3>
<h3 style="border-bottom: 1px solid #808080;"><strong>ISP:</strong> <?php echo $record['isp']; ?></h3>
<?php
} else {
echo '<p>Invalid data format</p><br>';
echo '<div class="diZFlexColumn diZmxAuto diZJustifyCenter diZItemsCenter">
<a class="diZButtonDenger " href="/tools/location-ip-to-location">Back</a>
</div>';
}
}
?>
</div>
<div class="diZFlexColumn diZmxAuto diZJustifyCenter diZItemsCenter">
<a class="diZButtonDenger " href="/tools/location-ip-to-location">Back</a>
</div>
<?php
// Echoing the data as JSON
// echo json_encode($data);
} catch (Exception $e) {
echo json_encode(['error' => $e->getMessage()]);
echo '<div class="diZFlexColumn diZmxAuto diZJustifyCenter diZItemsCenter">
<a class="diZButtonDenger " href="/tools/location-ip-to-location">Back</a>
</div>';
}
} else {
?>
<div class="diZMaxW600 diZw100 diZmxAuto toolsSection diZFlexColumn diZmy20"><br>
<form method="get" class="diZFlexColumn">
<label for="ip">IP Address:</label>
<div class="diZFlexColumn">
<input class="diZPadding5px" type="text" name="ip" id="ip" placeholder="Enter IP Address" />
<button class="diZmxAuto diZmt4" type="submit"><span>Check IP Details</span></button>
</div>
</form><br>
</div>
<!-- echo json_encode(['error' => 'Invalid parameters']); diZw100 -->
<?php
}
?>
<div>
<h3>Usage:</h3>
<ul>
<li><strong>Enter IP Address:</strong> Input the IP address you wish to check in the provided query parameter.</li>
<li><strong>Check Location:</strong> Send a request with the IP address to retrieve and display the geographical and ISP information.</li>
<li><strong>View Results:</strong> The location and ISP details will be displayed in a structured format, showing country, city, region, ASN, ISP, and other relevant information.</li>
</ul>
<h3>Features:</h3>
<ul>
<li>Uses MaxMind GeoLite2 databases for accurate geo-location and ISP information.</li>
<li>Displays detailed geographical information including continent, country, state, city, and region.</li>
<li>Provides additional information such as timezone, latitude, longitude, and postal code.</li>
<li>Displays Autonomous System Number (ASN) and Internet Service Provider (ISP) details.</li>
</ul>
<h3>Example Use Cases:</h3>
<ul>
<li>Network administrators tracking the location of IP addresses accessing their networks.</li>
<li>Security professionals analyzing IP addresses for potential threats.</li>
<li>Developers building applications that require IP-based location services.</li>
<li>Marketers tailoring content based on the geographic location of their audience.</li>
</ul>
</div>
</section>