Skip to content

Instantly share code, notes, and snippets.

@nagiyevelchin
Last active February 12, 2022 15:54
Show Gist options
  • Save nagiyevelchin/cf3e9c6b52cb20092b1b5672e42b239c to your computer and use it in GitHub Desktop.
Save nagiyevelchin/cf3e9c6b52cb20092b1b5672e42b239c to your computer and use it in GitHub Desktop.
Get clients IP address in PHP
<?php
/**
* Retuns client's IP address
* @return string
* @since 6/16/11 12:11 PM
*/
function getClientIPAddress() {
if (getenv("HTTP_CLIENT_IP")) {
$ip = getenv("HTTP_CLIENT_IP");
} else {
if (getenv("HTTP_X_FORWARDED_FOR")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
} else {
if (getenv("REMOTE_ADDR")) {
$ip = getenv("REMOTE_ADDR");
} else {
$ip = "UNKNOWN";
}
}
}
return $ip;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment