Skip to content

Instantly share code, notes, and snippets.

@coccoinomane
Last active May 23, 2017 13:04
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 coccoinomane/4c420776dc16d80ea772aff06d3e1ef4 to your computer and use it in GitHub Desktop.
Save coccoinomane/4c420776dc16d80ea772aff06d3e1ef4 to your computer and use it in GitHub Desktop.
<?php
/**
* Return the client's IP address.
*
* Returns the IP address if found, an empty string if not. The
* output is santized via FILTER_VALIDATE_IP.
*
* The algorithm uses the HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR
* globals to infer the IP address; if they are not available (as
* it is the case in most cases), it uses the REMOTE_ADDR global.
*
* Do not use this function to grant access or privileges to
* IP addresses, because the HTTP_CLIENT_IP and
* HTTP_X_FORWARDED_FOR globals can be easily spoofed.
*
* On the other hand, the REMOTE_ADDR global is very difficult
* to spoof. However, when the client is beyond a proxy, it isn't
* necessarily the correct IP.
*
* See here for more details: http://stackoverflow.com/questions/
* 3003145/how-to-get-the-client-ip-address-in-php
*
* Created by Guido W. Pettinari on 05.09.2016.
* Latest version here:
* https://gist.github.com/coccoinomane/4c420776dc16d80ea772aff06d3e1ef4
*/
function getClientIp ()
{
if (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
else {
return '';
}
return filter_var($ip, FILTER_VALIDATE_IP);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment