different method for get ip ip address of user/client

In this tutorial we will show you how to get ip address of user/client. we will show you different method for get ip ip address of user/client.

METHOD – 1

$user_ip_address = "";
$user_ip_address = $_SERVER['HTTP_CLIENT_IP'];

METHOD – 2

function get_client_ip()
{
$user_ip_address = "";
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
// For HTTP_CLIENT_IP type Ip
$user_ip_address = $_SERVER['HTTP_CLIENT_IP'];
}
else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
// to it for from proxy IP Address
$user_ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
// to check ip is pass from proxy
$user_ip_address = $_SERVER['REMOTE_ADDR'];
}
return $user_ip_address;
}

METHOD – 3

getenv() is use for get value of environment of variable in php.

// get_client_ip() Function to IP address of client/user
function get_client_ip() {
$user_ip_address = "";
if (getenv('HTTP_CLIENT_IP'))
{
// For HTTP_CLIENT_IP type Ip
$user_ip_address = getenv('HTTP_CLIENT_IP');
}
else if(getenv('HTTP_X_FORWARDED_FOR'))
{
// For HTTP_X_FORWARDED_FOR type Ip
$user_ip_address = getenv('HTTP_X_FORWARDED_FOR');
}
else if(getenv('HTTP_X_FORWARDED'))
{
// For HTTP_X_FORWARDED type Ip
$user_ip_address = getenv('HTTP_X_FORWARDED');
}
else if(getenv('HTTP_FORWARDED_FOR'))
{
// For HTTP_FORWARDED_FOR type Ip
$user_ip_address = getenv('HTTP_FORWARDED_FOR');
}
else if(getenv('HTTP_FORWARDED'))
{
// For HTTP_FORWARDED type Ip
$user_ip_address = getenv('HTTP_FORWARDED');
}
else if(getenv('REMOTE_ADDR'))
{
// For REMOTE_ADDR type Ip
$user_ip_address = getenv('REMOTE_ADDR');
}
else
{
// If not able get ip
$user_ip_address = 'UNKNOWN';
}
return $user_ip_address;
}

METHOD – 4

$_SERVER is use for an array contains server variables created by the web server in php.

// get_client_ip() Function to IP address of client/user
function get_client_ip() {
$user_ip_address = "";
if (isset($_SERVER['HTTP_CLIENT_IP']))
{
// For HTTP_CLIENT_IP type Ip
$user_ip_address = $_SERVER['HTTP_CLIENT_IP'];
}
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
// For HTTP_X_FORWARDED_FOR type Ip
$user_ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else if(isset($_SERVER['HTTP_X_FORWARDED']))
{
// For HTTP_X_FORWARDED type Ip
$user_ip_address = $_SERVER['HTTP_X_FORWARDED'];
}
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
{
// For HTTP_FORWARDED_FOR type Ip
$user_ip_address = $_SERVER['HTTP_FORWARDED_FOR'];
}
else if(isset($_SERVER['HTTP_FORWARDED']))
{
// For HTTP_FORWARDED type Ip
$user_ip_address = $_SERVER['HTTP_FORWARDED'];
}
else if(isset($_SERVER['REMOTE_ADDR']))
{
// For REMOTE_ADDR type Ip
$user_ip_address = $_SERVER['REMOTE_ADDR'];
}
else
{
// If not able get ip
$user_ip_address = 'UNKNOWN';
}
return $user_ip_address;
}

This function is use check enter ip address is validate. It will check ip have class validate.It will check valid ip address class A To E

Class Theoretical Address Range Binary Start
A 0.0.0.0 to 127.255.255.255
B 128.0.0.0 to 191.255.255.255 10
C 192.0.0.0 to 223.255.255.255 110
D 224.0.0.0 to 239.255.255.255 1110
E 224.0.0.0 to 239.255.255.255 1110

Class E has not equipped with any subnet mask.

function check_validate_ip($user_ip_address) {
if (strtolower($user_ip_address) === 'UNKNOWN')
return false;

// Convert Internet Protocol(IPv4)
// dotted address into a long integer.
$user_ip_address = ip2long($user_ip_address);

// if the ip is set and not equivalent to 255.255.255.255
if ($user_ip_address !== false && $user_ip_address !== -1)
{
$user_ip_address = sprintf('%u', $user_ip_address);
// do private network range checking
if ($user_ip_address >= 0 && $user_ip_address <= 50331647)
{
return false;
}
if ($user_ip_address >= 167772160 && $user_ip_address <= 184549375)
{
return false;
}
if ($user_ip_address >= 2130706432 && $user_ip_address <= 2147483647)
{
return false;
}
if ($user_ip_address >= 2851995648 && $user_ip_address <= 2852061183)
{
return false;
}
if ($user_ip_address >= 2886729728 && $user_ip_address <= 2887778303)
{
return false;
}
if ($user_ip_address >= 3221225984 && $user_ip_address <= 3221226239)
{
return false;
}
if ($user_ip_address >= 3232235520 && $user_ip_address <= 3232301055)
{
return false;
}
if ($user_ip_address >= 4294967040)
{
return false;
}
}
return true;
}

Leave a Comment

Your email address will not be published. Required fields are marked *

57  +    =  64

We accept paid advance and paid guest Posting on our Site : Contact US