Skip to content

Instantly share code, notes, and snippets.

@alfchee
Last active August 29, 2015 14:24
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 alfchee/93b71e5897ceca612048 to your computer and use it in GitHub Desktop.
Save alfchee/93b71e5897ceca612048 to your computer and use it in GitHub Desktop.
cIP
<?php
/**
$Id: cIP.php,v 1.0 2010/10/14 20:07:04 laudarch Exp $
Author: Archzilon Eshun-Davies
URL: http://www.phpclasses.org/package/8494-PHP-Determine-the-IP-address-of-the-user-from-headers.html
License: BSD License
PHP v: 5.0
*/
# Set to true if you want to test the class
$TEST = false;
interface iIP {
public static function getusrip();
}
class cIP implements iIP {
/**
* Returns User IP Address
* @params
* IN: NONE
* OUT: ip address(0.0.0.0)
*/
public static function getusrip()
{
$ip = null;
if ((isset($_SERVER['HTTP_X_FORWARDED_FOR'])) &&
(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif ((isset($_SERVER['HTTP_CLIENT_IP'])) &&
(!empty($_SERVER['HTTP_CLIENT_IP']))) {
$ip = explode(".", $_SERVER['HTTP_CLIENT_IP']);
$ip = "{$ip[3]}.{$ip[2]}.{$ip[1]}.{$ip[0]}";
} elseif ((!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) &&
(empty($_SERVER['HTTP_X_FORWARDED_FOR'])) &&
(!isset($_SERVER['HTTP_CLIENT_IP'])) &&
(empty($_SERVER['HTTP_CLIENT_IP'])) &&
(isset($_SERVER['REMOTE_ADDR'])) ) {
$ip = ($_SERVER['REMOTE_ADDR']);
} else {
// ip is null
}
return ($ip);
}
}
if ($TEST) {
$ip = cIP::getusrip();
print "ip: $ip\n<br />";
print "TEST done!";
exit;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment