Skip to content

Instantly share code, notes, and snippets.

@alfchee
Last active Aug 29, 2015
Embed
What would you like to do?
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