Skip to content

Instantly share code, notes, and snippets.

@ljungmark
Created September 23, 2016 17:15
Show Gist options
  • Save ljungmark/482049a8df94f33fdce5501a7c36f289 to your computer and use it in GitHub Desktop.
Save ljungmark/482049a8df94f33fdce5501a7c36f289 to your computer and use it in GitHub Desktop.
Convert a UA provided IPv4-address to it's hexadecimal value
<?php
function ip4_to_hex($ip_address = null) {
if ($ip_address === null || filter_var($ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) return 'Unable to read IP address';
$fragments = explode('.', $ip_address);
return sprintf('%02x%02x%02x%02x', $fragments[0], $fragments[1], $fragments[2], $fragments[3]);
}
print ip4_to_hex($_SERVER['REMOTE_ADDR']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment