Created
June 7, 2021 22:37
-
-
Save editicalu/4d555d408120fe7adb70db3d9719338d to your computer and use it in GitHub Desktop.
fourorsix: is this viewed using IPv4 or IPv6?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://www.php.net/manual/en/function.str-contains.php#125977 | |
if (!function_exists('str_contains')) { | |
function str_contains($haystack, $needle) { | |
return $needle !== '' && mb_strpos($haystack, $needle) !== false; | |
} | |
} | |
$ip = $_SERVER['REMOTE_ADDR']; | |
$isipv6 = str_contains($ip, ':'); | |
$data = Array( | |
'ip_address' => $ip, | |
'ip_version' => $isipv6 ? 6 : 4 | |
); | |
// JSON output | |
header("Content-type:application/json"); | |
$json = json_encode($data); | |
echo $json; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment