What is IP Address?

An Internet Protocol address (IP address) is a logical numeric address that is assigned to every single computer, printer, switch, router or any other device that is part of a TCP/IP-based network.

Addresses in IPv4 are 32-bits long. This allows for a maximum of 4,294,967,296 (232) unique addresses. Addresses in IPv6 are 128-bits, which allows for 3.4 x 1038 (2128) unique addresses.

The total usable address pool of both versions is reduced by various reserved addresses and other considerations.

IP addresses are binary numbers but are typically expressed in decimal form (IPv4) or hexadecimal form (IPv6) to make reading and using them easier for humans.

The Internet Protocol (IP)

IP stands for Internet Protocol and describes a set of standards and requirements for creating and transmitting data packets, or datagrams, across networks. The Internet Protocol (IP) is part of the Internet layer of the Internet protocol suite. In the OSI model, IP would be considered part of the network layer. IP is traditionally used in conjunction with a higher-level protocol, most notably TCP. The IP standard is governed by RFC 791.

Real Also: How to remove index.php from URL in CodeIgniter

How to Get IP Address of Visitor using PHP

As a web developer sometimes we need to collect the visitor IP address to track visitor activity and for other reason.

The easiest way to get the visitor IP address is using the $_SERVER[‘REMOTE_ADDR’] variables in PHP. It will returns the IP address of the user from which viewing the current page.

An example:

echo 'User IP - '.$_SERVER['REMOTE_ADDR'];

However, sometimes this does not return the correct IP address of the visitor, so we can use some other server variables to get the IP address. The reason behind this is to use Proxy. So we can use the following code to get real IP address of visitor in PHP.

$_SERVER is an array that contains server variables created by the web server.

function get_ip() {
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP']))
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_X_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if(isset($_SERVER['REMOTE_ADDR']))
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}
echo 'User Real IP - '.get_ip();

LEAVE A REPLY

Please enter your comment!
Please enter your name here