Skip to content

Instantly share code, notes, and snippets.

@editicalu
Created June 7, 2021 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save editicalu/4d555d408120fe7adb70db3d9719338d to your computer and use it in GitHub Desktop.
Save editicalu/4d555d408120fe7adb70db3d9719338d to your computer and use it in GitHub Desktop.
fourorsix: is this viewed using IPv4 or IPv6?
<?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