Skip to content

Instantly share code, notes, and snippets.

@raishid
Created August 27, 2024 02:19
Show Gist options
  • Save raishid/5a74766c9be9b659c0839677839f6cd3 to your computer and use it in GitHub Desktop.
Save raishid/5a74766c9be9b659c0839677839f6cd3 to your computer and use it in GitHub Desktop.
<?php
function get_ip() {
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip) {
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
return $ip;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment