Skip to content

Instantly share code, notes, and snippets.

@Mossuru777
Created February 26, 2020 04:59
Show Gist options
  • Save Mossuru777/b751115fde9f49b32a6f157210cacedf to your computer and use it in GitHub Desktop.
Save Mossuru777/b751115fde9f49b32a6f157210cacedf to your computer and use it in GitHub Desktop.
<?php
switch ($_SERVER["REQUEST_METHOD"]) {
case "GET":
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=utf-8");
header("Cache-Control: no-store");
$remote_addr = $_SERVER["REMOTE_ADDR"];
$remote_host = $_SERVER["REMOTE_HOST"];
if ($remote_host === $remote_addr) {
$remote_host = gethostbyaddr($remote_addr);
}
print(json_encode(
[
"ip" => $remote_addr,
"hostname" => $remote_host
]
));
break;
case "OPTIONS":
header("Access-Control-Allow-Origin: *");
header("Access-Control-Max-Age: 0");
header("Access-Control-Allow-Methods: GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");
http_response_code(204);
break;
default:
http_response_code(405);
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment