aboutsummaryrefslogtreecommitdiff
path: root/ifconfig.php
diff options
context:
space:
mode:
authorChristian Cleberg <hello@cleberg.net>2023-05-24 21:18:39 -0500
committerChristian Cleberg <hello@cleberg.net>2023-05-24 21:18:39 -0500
commitb3286eb015079950554508e0724fd5f12c71833f (patch)
tree8ac93accb2465c0b3dc34e78686beeac879eaf68 /ifconfig.php
parent864f1349e47f924bcde20c1cc83d838b5c5c7316 (diff)
downloadifconfig.php-b3286eb015079950554508e0724fd5f12c71833f.tar.gz
ifconfig.php-b3286eb015079950554508e0724fd5f12c71833f.tar.bz2
ifconfig.php-b3286eb015079950554508e0724fd5f12c71833f.zip
add geolocation functionality
Diffstat (limited to 'ifconfig.php')
-rw-r--r--ifconfig.php56
1 files changed, 39 insertions, 17 deletions
diff --git a/ifconfig.php b/ifconfig.php
index 8ece6ba..fc4dd30 100644
--- a/ifconfig.php
+++ b/ifconfig.php
@@ -1,28 +1,50 @@
<?php
+require 'vendor/autoload.php';
+use MaxMind\Db\Reader;
+
+function loadGeo(string $ipAddress): ?array
+{
+ $databaseFile = 'GeoLite2-City.mmdb';
+ $reader = new Reader($databaseFile);
+ $results = $reader->get($ipAddress);
+ $reader->close();
+
+ return $results;
+}
+
+// Set top-level variables for HTML
$site_title = 'ip.cleberg.net';
$site_owner_name = 'Christian Cleberg';
$site_owner_url = 'https://cleberg.net';
$source_repository = 'https://git.sr.ht/~cmc/ifconfig.php';
+// Extract geolocation
+$geo = loadGeo($_SERVER['REMOTE_ADDR']);
+
// Create array of values
$user = array(
- 'ip' => $_SERVER['REMOTE_ADDR'],
- 'host' => (isset($_SERVER['REMOTE_ADDR']) ? gethostbyaddr($_SERVER['REMOTE_ADDR']) : ""),
- 'port' => $_SERVER['REMOTE_PORT'],
- 'ua' => $_SERVER['HTTP_USER_AGENT'],
- 'lang' => $_SERVER['HTTP_ACCEPT_LANGUAGE'],
- 'mime' => $_SERVER['HTTP_ACCEPT'],
- 'encoding' => $_SERVER['HTTP_ACCEPT_ENCODING'],
- 'charset' => $_SERVER['HTTP_ACCEPT_CHARSET'],
- 'connection' => $_SERVER['HTTP_CONNECTION'],
- 'cache' => $_SERVER['HTTP_CACHE_CONTROL'],
- 'cookie' => $_SERVER['HTTP_COOKIE'],
- 'referer' => $_SERVER['HTTP_REFERER'],
- 'real_ip' => $_SERVER['HTTP_X_REAL_IP'],
- 'fwd_ip' => $_SERVER['HTTP_X_FORWARDED_FOR'],
- 'fwd_host' => (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? gethostbyaddr($_SERVER['HTTP_X_FORWARDED_FOR']) : ""),
- 'dnt' => $_SERVER['HTTP_DNT']
+ 'ip' => $_SERVER['REMOTE_ADDR'],
+ 'host' => (isset($_SERVER['REMOTE_ADDR']) ? gethostbyaddr($_SERVER['REMOTE_ADDR']) : ""),
+ 'port' => $_SERVER['REMOTE_PORT'],
+ 'ua' => $_SERVER['HTTP_USER_AGENT'],
+ 'lang' => $_SERVER['HTTP_ACCEPT_LANGUAGE'],
+ 'mime' => $_SERVER['HTTP_ACCEPT'],
+ 'encoding' => $_SERVER['HTTP_ACCEPT_ENCODING'],
+ 'charset' => $_SERVER['HTTP_ACCEPT_CHARSET'],
+ 'connection' => $_SERVER['HTTP_CONNECTION'],
+ 'cache' => $_SERVER['HTTP_CACHE_CONTROL'],
+ 'cookie' => $_SERVER['HTTP_COOKIE'],
+ 'referer' => $_SERVER['HTTP_REFERER'],
+ 'real_ip' => $_SERVER['HTTP_X_REAL_IP'],
+ 'fwd_ip' => $_SERVER['HTTP_X_FORWARDED_FOR'],
+ 'fwd_host' => (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? gethostbyaddr($_SERVER['HTTP_X_FORWARDED_FOR']) : ""),
+ 'dnt' => $_SERVER['HTTP_DNT'],
+ 'continent_code' => $geo['continent']['code'],
+ 'continent_name' => $geo['continent']['names']['en'],
+ 'country_code' => $geo['country']['geoname_id'],
+ 'country_iso' => $geo['country']['iso_code'],
+ 'country_name' => $geo['country']['names']['en']
);
// Check request (ex. ifconfig.php?q=ip)
@@ -104,7 +126,7 @@ elseif (isset($query) && (($query=="text") || ($query=="all"))) {
$date = date("Y");
echo <<<EOD
<br>
- <p><small>Copyright &copy; {$date} <a href="{$site_owner_url}">{$site_owner_name}</a>.
+ <p><small>Copyright &copy; {$date} <a href="{$site_owner_url}">{$site_owner_name}</a>
<br><a target="_blank" rel="noreferrer" href="{$source_repository}">Source Code</a></small></p>
</body>
</html>