Skip to content

Instantly share code, notes, and snippets.

@vhdm
Last active September 6, 2015 15:19
Show Gist options
  • Save vhdm/12e54e50f1d4dbc7a3e2 to your computer and use it in GitHub Desktop.
Save vhdm/12e54e50f1d4dbc7a3e2 to your computer and use it in GitHub Desktop.
Get ip address in php
<?php
function get_ip_address()
{
if (isset($_SERVER)) {
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && ip2long($_SERVER["HTTP_X_FORWARDED_FOR"]) !== false) {
$ipadres = $_SERVER["HTTP_X_FORWARDED_FOR"];
} elseif (isset($_SERVER["HTTP_CLIENT_IP"]) && ip2long($_SERVER["HTTP_CLIENT_IP"]) !== false) {
$ipadres = $_SERVER["HTTP_CLIENT_IP"];
} else {
$ipadres = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv('HTTP_X_FORWARDED_FOR') && ip2long(getenv('HTTP_X_FORWARDED_FOR')) !== false) {
$ipadres = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('HTTP_CLIENT_IP') && ip2long(getenv('HTTP_CLIENT_IP')) !== false) {
$ipadres = getenv('HTTP_CLIENT_IP');
} else {
$ipadres = getenv('REMOTE_ADDR');
}
}
return $ipadres;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment